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 ...