Lucas Zientek
Lucas Zientek

Reputation: 294

NodeJs post request on a api accents error

I have a problem when I post a json with accents (like éè...) the server send back a 400 bad request error. I don't know how to solve this problem.

bodyStr ='{"name":"ééé"}';
//headers
var headers = {
    'Content-Type': 'application/json',
    'Content-Length': bodyStr.length,
    'X-Key' : this.xkey
};

//http request
request({
    uri : this.httpHost + path, 
    method: method, 
    headers : headers, 
    body : bodyStr
},  
function (error, response, body) {
    if (!error && response.statusCode < 400) {
        if (typeof successCallback == "function") {
            successCallback(JSON.parse(body), response);
        }
    } else {
        if (typeof errorCallback == "function") {
            errorCallback(error || response.body || response.statusCode, response);
        }
    }
}
);

Upvotes: 2

Views: 1203

Answers (2)

Lucas Zientek
Lucas Zientek

Reputation: 294

To solve this problem you need to convert the string to utf-8 with a module like iconv-lite(http://github.com/ashtuchkin/iconv-lite) like this :

var iconv = require('iconv-lite');
bodyStr = iconv.encode(bodyStr, 'utf-8');

Upvotes: 2

Thierry Templier
Thierry Templier

Reputation: 202296

Do you implement the RESTful service? Do you have additional hints the regarding within the response content?

Perhaps setting the character set (latin1) within the header Content-type could fix the issue:

Content-Type: application/json; charset=iso-8859-1

Hope it helps you, Thierry

Upvotes: 1

Related Questions