简体   繁体   English

node.js正文解析器对Content-Type:x-www-form-urlencoded和Form-data JSON的错误解释

[英]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>&nbsp;sitedemo&nbsp;&nbsp;&nbsp;&nbsp;</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;':'',
'&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.

相关问题 如何在 node.js 中发布内容类型 =&#39;application/x-www-form-urlencoded&#39; 的数据 - how to post data in node.js with content type ='application/x-www-form-urlencoded' request.post继续覆盖内容类型作为content-type:application / x-www-form-urlencoded当指定表单数据时 - request.post continues to override content-type as content-type: application/x-www-form-urlencoded when form-data is being specified 如何将Content-Type:appication / x-www-form-urlencoded更改为application / json? - How to change Content-Type: appication/x-www-form-urlencoded to application/json? 节点js如何识别邮递员扩展客户端请求来自“表单数据”或“ x-www-form-urlencoded” - node js how to identify postman extention client request came from “form-data” or “x-www-form-urlencoded” x-www-form-urlencoded 格式 - 在 node.js 中使用 https - x-www-form-urlencoded format - using https in node.js 具有 application/x-www-form-urlencoded 格式的 Node.js Axios POST 请求? - Node.js Axios POST request with application/x-www-form-urlencoded format? Node.js:Axios 与使用 x-www-form-urlencoded 获取 - Node.js: Axios vs Fetch using x-www-form-urlencoded 使用application / x-www-form-urlencoded使用node.js在发布请求中发送数组 - Send Array in post request using node.js using application/x-www-form-urlencoded 如何在nodejs中支持application / json,application / x-www-form-urlencoded和multipart / form-data的请求或响应主体 - how to support request or response bodies for application/json, application/x-www-form-urlencoded and multipart/form-data in nodejs 在请求中解码 JSON object,其中内容类型为“application/x-www-form-urlencoded” - Decode JSON object in req where content type 'application/x-www-form-urlencoded'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM