[英]node.js body-parser bad interpretation of Content-Type:x-www-form-urlencoded and Form-data JSON
I have a client request with 我有一个客户要求
Content-type: Content-Type:application/x-www-form-urlencoded; charset=UTF-8
And Form-Data like this (json): 像这样的Form-Data(json):
{"jsonrpc":"2.0","method":"print","params":{"id":"lp0","doc":"<section>
<p> sitedemo </p><br><barcode> CLODGCGMM
</barcode><br><br><hr><drawer></drawer><br></section>"},"id":1501151330950}
The node.js server use body-parser middleware like this : node.js服务器使用主体解析器中间件,如下所示:
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
A console.log(request.body) give me something like this: console.log(request.body)给我这样的东西:
{ '{"jsonrpc":"2.0","method":"print","params":{"id":"lp0","doc":"<section>
<p>': '',
'nbsp;': '',
'nbsp;sitedemo': '',
'nbsp;':'',
' </p><br><barcode>':''
'.......}'}
A json object is built having keys made by lines and values null . 构建一个json对象,其中包含由行和值null组成的键。
How can i retrieve the exact object send by client (i have no access to client) 我如何检索客户端发送的确切对象(我无法访问客户端)
Thank you. 谢谢。
The client is broken when it's stating in the headers that the request body is URL-encoded but it's sending JSON. 当客户端在标头中声明请求正文已进行URL编码但正在发送JSON时,客户端就损坏了。
If that's really the situation, you need to prevent those requests from being decoded by body-parser
, and do the decoding manually. 如果确实是这种情况,则需要防止body-parser
解码这些请求,并手动进行解码。
Instead of this: 代替这个:
app.use(bodyParser.urlencoded({ extended: true }));
Try this: 尝试这个:
app.use(
bodyParser.raw({ type : 'application/x-www-form-urlencoded' }),
function(req, res, next) {
try {
req.body = JSON.parse(req.body)
} catch(e) {
req.body = require('qs').parse(req.body.toString());
}
next();
}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.