Express.js:将 req.body 转换为 POST 编码的字符串
我正在使用 express.bodyParser 中间件,并且尝试将 req.body 对象转换为 POST 编码的字符串。有办法做到这一点吗?
示例:
Name: Jonathan Doe
Age: 23
Formula: a + b == 13%!
变为:
Name=Jonathan+Doe&Age=23&Formula=a+%2B+b+%3D%3D+13%25%21
I'm using the express.bodyParser middleware and I'm trying to convert req.body object into a POST encoded string. Is there a way of doing this?
Example:
Name: Jonathan Doe
Age: 23
Formula: a + b == 13%!
Becomes:
Name=Jonathan+Doe&Age=23&Formula=a+%2B+b+%3D%3D+13%25%21
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Node 有一个用于此目的的模块。
但 connect/express 无论如何都会将原始主体存储在
req.rawBody
中。Node has a module for this.
But connect/express store the raw body in
req.rawBody
anyway.我认为这应该相当简单 - 您应该能够像在浏览器中一样进行操作。此函数将对象/数组的所有字符串/数字成员转换为可用作 POST 正文的字符串:
如果您想处理子数组/子对象,该函数会变得更复杂,但对于您上面描述的内容我认为这应该可以解决问题。
I think this should be fairly simple - you should be able to do it the same way you would do in a browser. This function converts all string/number members of an object/array to a string that can be used as a POST body:
If you want to handle sub-arrays/sub-objects the function would get more complicated, but for what you describe above I think that should do the trick.