What to note when post Chinese in the node. JS Express Environment

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.