当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Invalid JSON Error at POST/PUT json with special characters using nodejs http.request()

问题描述:

I'm using node.js http module to PUT a json data into CouchDB. This json contains a special character "ä" which causes CouchDB responding with "invalid_json" error. Once this special char is removed or replaced, the data is saved successfully. My node.js code:

var data = {

"a": "Gerät"

};

var stringifiedData = JSON.stringify(data);

var http = http || require("http");

var requestOptions = {

"host": "localhost",

"port": "5984",

"method": "PUT",

"headers": {

"Content-Type": "application/json",

"Content-Length": stringifiedData.length

},

"path": "/testdb/test"

};

var req = http.request(requestOptions, function (res) {

res.setEncoding("utf8");

res.on("data", function (chunk) {

console.log("Response: " + chunk);

});

});

console.log("stringifiedData: ", stringifiedData);

req.end(stringifiedData);

The interesting thing is, if I save this data into a json file and use curl command to PUT it into CouchDB, the data is saved without any problem.

curl -X PUT -d @test.json http://localhost:5984/testdb/test

Do I miss some encoding configurations at using nodejs http.request()? I tried with converting "Gerät" to 'utf8' encoding: Buffer.from("Gerät", "latin1").toString("utf8); but it didn't help.

The same issue exists with POSTing a json containing this special character to CouchDB /_bulk_docs API.

网友答案:

The issue is that you are including the string length for the Content-Length, but not the length in bytes -- special characters can have longer lengths. Try changing the line to this:

var stringifiedData = new Buffer(JSON.stringify(data));

This should allow the content length to be correct.

Forum post discussing issue

分享给朋友:
您可能感兴趣的文章:
随机阅读: