Recently in the NUXT project began to move some cross-domain requests to express, in fact, similar to other languages, HTTP object requests, receive requests are written very smoothly. Most of the previously used request parts are get requests, so in addition to attention to the coding problem, basically what the interface is written quickly, but in the post request due to the calculation of content-length, there is a problem due to the Chinese calculation of the length of the request failed.
var bodystring = json.stringify (obj)
var options = { ' POST ', ' mt.emoney.cn ', +, path: ' * * * * * ', headers: { ' content-type ': ' Application/json;charset=utf-8 ', ' Content-length ': Bodystring.length } }
When the above request is initiated, if there is a property inside the Obj object containing the Chinese, bodystring.length has a problem with the calculation, the following red is a scheme.
var bodystring = json.stringify (obj)
bodystring = new Buffer (bodystring) var options = { ' POST ', ' Mt.emoney.cn ', a, path: '/platform/stock/markstock ', headers: { ' Content-type ': ' Application/json;charset=utf-8 ', ' content-length ': Bodystring.length } }
Here's another way to get the length of the program.
Buffer.bytelength (data, ' UTF8 ');
What to note when post Chinese in the node. JS Express Environment