3

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

nufuk
  • 41
  • 5
  • The stack trace seems to be from the client, not the server. – robertklep May 07 '17 at 11:17
  • Thank you, but why is my client crashing? There is no Problem when I make the GET with my browser. Do I need some kind of character en/decodingß – nufuk May 07 '17 at 14:03
  • It might be the server that's crashing, causing the client to crash. In any case, you should use UTF-8 encoding. – robertklep May 07 '17 at 14:07

1 Answers1

1

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

nufuk
  • 41
  • 5