NodeJS REST Turkish character, Socket hang up

My REST service:

const
  express = require('express'),
  app = express();
  
  ...
  
 app.get('/turkish-escape/:id', function(req, res) {
  console.log("converting turkish special characters:");
  console.log("\u011f \u011e \u0131 \u0130 \u00f6 \u00d6 \u00fc \u00dc \u015f \u015e \u00e7 \u00c7");
  let char = req.params.id;
  
 .....//DO stuff
      

Run codeHide result


When I try to GET the service in the browser, I don't get any errors: http: // localhost: 8081 / turkish-escape / ğ /

When I try to get the result with my REST client, I run into some problems with some Turkish special characters.

Client code:

let currentChar = text[i];
  let
    request = require('request'),
    options = {
      method: 'GET',
      url: 'http://localhost:8081/turkish-escape/' + currentChar + "/"
    };
    request(options, function(err, res, body) {
      if(err) {
        throw Error(err);
      } else {
        body = JSON.parse(body);
        console.log(body.convertedChar);
        newText += body.convertedChar;
      }
    });
      

Run codeHide result


When I call the client with "ü" it works fine, but if I call it "ğ" it works. This is the call and the stack trace:

./turkishTextConverter.js ğ
/pathtofile/turkishTextConverter.js:25
    throw Error(err);
    ^

Error: Error: socket hang up
at Request._callback (/pathtofile/turkishTextConverter.js:25:15)
at self.callback (/pathtofile/node_modules/request/request.js:188:22)
at emitOne (events.js:96:13)
at Request.emit (events.js:191:7)
at Request.onRequestError (/pathtofile/node_modules/request/request.js:884:8)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:191:7)
at Socket.socketOnEnd (_http_client.js:394:9)
at emitNone (events.js:91:20)
at Socket.emit (events.js:188:7)

      

As you can see from the stack, it never reaches even the first console.log statement of the REST service

+3


source to share


1 answer


The solution was url encoding:

...
url: 'http://localhost:8081/turkish-escape/' + currentChar + "/"
...

      

to



...
url: encodeURI('http://localhost:8081/turkish-escape/' + currentChar + '/')
...

      

and now the client won't crash anymore

+1


source







All Articles