Saturday 6 May 2017

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



via nufuk

No comments:

Post a Comment