Request access to Nodejs 415 error using GBK encoding: error:unsupported charset at Urlencodedparser ...

Source: Internet
Author: User
Tags unsupported

Recently encountered a problem, third-party users sent us a callback address to send a request has been reported 415 error, the results found that they are using GBK encoding request, and we use the node + EXPRESS4 program, does not support GBK encoding request.

The problem is in the following section of code.

false // This line

View the Body-parser source code, find the problem caused by a section of the following

var charset = Typer.parse (req). Parameters.charset | | ' Utf-8 '    if (charset.tolowercase ()!== ' Utf-8 ') {      var err = new Error (' Unsupported charset ')      err.status = 415< C7/>next (Err)      return    }

Online Search a lot of information, some said to comment out the source of this judgment, some said to replace the Body-parser version, and so on, but I think these methods are not good.

Then I intercepted each other's request header, and found that the key sentence was this.

"Content-type": "APPLICATION/X-WWW-FORM-URLENCODED;CHARSET=GBK"

Fortunately, the parameters they requested are both English and digital, and I think I can replace the GBK with UTF8, so I wrote the following code to solve the problem.

// using the GBK encoding, will be an error, here intercept processing app.use (function  (req, res, next) {    if (req.headers[' Content-type '] && req.headers[' content-type '].indexof (' GBK ') >-1) {        req.headers[' Content-type '] = req.headers[' Content-type '].replace (' GBK ', ' UTF-8 ');    }     false // This line

Request access to Nodejs 415 error using GBK encoding: error:unsupported charset at Urlencodedparser ...

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.