Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS REST turkish character, Socket hang up

My REST service is the following:

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

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 encounter 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;
      }
    });

When I call the client with an 'ü' it works fine, but if I call it with an 'ğ' it crashes. This is the call and 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 see from the stack it never reaches even the first console.log statement of the REST Service

like image 821
nufuk Avatar asked Mar 23 '26 09:03

nufuk


1 Answers

The solution was to encodeURI the url:

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

to

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

and now the client won't crash anymore

like image 120
nufuk Avatar answered Mar 25 '26 23:03

nufuk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!