oderfla oderfla - 1 month ago 22
HTTP Question

Node requestify fails when body includes special chars

Im using node.js and a library called requestify.
The code looks like this:

console.log('body');
console.log(body);
return new Promise(function (f, r) {
requestify.request(myurl, {
method: 'POST',
body : body,
headers : {
"Content-Type" : "application/json; charset=utf-8"
}
})
.then(function (response) {
f({messge : "Success"});
}, function (err) {
console.log(err);
err.code == 405 ? r(err.body) : r({ success: false, message: err });
});
});


When the request contains special characters (in this case swedish characters) then it throws an error.

Sending exactly the same request without the swedish characters, the request succedes.

I dont really have access to the server that is handling the request that's why it is hard to debug.
But I print the body before sending the request and I cannot see any strange things in it.
Here are the 2 different body-requests:

//FAILURE

{ eventType: 'createUser',
eventId: '1e2ca3b4-0f6b-4ed9-ae79-2e112c8d693c',
version: '0.1',
date: 2016-10-07T06:42:26.209Z,
attributes:
{ userName: 'akeandersson@gmail.se',
email: 'akeandersson@gmail.se',
firstName: 'Åke',
lastName: 'Andresson',
phoneNumber: '1',
identifier: '198509120328',
assignment: 'se.me',
role: [ 'se.me.role.user' ] },
metadata: {} }


//SUCCESS

{ eventType: 'createUser',
eventId: '1e2ca3b4-0f6b-4ed9-ae79-2e112c8d693c',
version: '0.1',
date: 2016-10-07T06:44:09.857Z,
attributes:
{ userName: 'akeandersson212@gmail.se',
email: 'akeandersson212@gmail.se',
firstName: 'Ake',
lastName: 'Andresson',
phoneNumber: '9',
identifier: '196606095823',
assignment: 'se.me',
role: [ 'se.me.role.user' ] },
metadata: {} }


I also put "; charset=utf-8" in the header's content-type (by default it is already utf-8 according to requestify documentation).
But still the same result. Any idea how I could debug this?

This is what the server returns when the request fails:

{
code: 400,
headers:
{ connection: 'close',
'content-type': 'text/plain',
'content-length': '244',
date: 'Fri, 07 Oct 2016 06:42:27 GMT' },
body: 'Unexpected end-of-input: expected close marker for OBJECT (from [Source: ServletInputStreamImpl@75eb88e4; line: 1, column: 0])\n at [Source: ServletInputStreamImpl@75eb88e4; line: 1, column: 767]' }

Answer

I guess, you need to upload your requestify module to the most recent version. I use version 0.2.3 which doesn't throw me any error.

Assuming that you encounter an error while trying to read a norwegian (or) swedish character, I have tried reading a website which has norwegian characters in it, and with the below code, I'm able to output it to the console without any error. Same with a swedish page too. I'm using 0.2.3 version though (which is the most recent version). I have not set the encoding to UTF-8 too.

var requestify = require('requestify'); 

requestify.get('http://www.norwegian4people.com/lesson.php?id=41').then(function(response) {
    // Get the response body 
    console.log(response.getBody());
});

Hope this helps!

Comments