简体   繁体   English

如何在 express js 中解析多部分/混合请求正文?

[英]How do I parse a multipart/mixed request body in express js?

How do I parse a request body with content type: multipart/mixed in express?如何解析内容类型为 multipart/mixed in express 的请求正文?

A simple example of such request is below:此类请求的一个简单示例如下:

curl -H "Content-Type: multipart/mixed" -F "request=@body.json;type=application/json" -F "file1=@test.xml" -X POST https://mysimpleapi.com

Body.json file is a simple json Body.json 文件是一个简单的 json

[{
    "name": "Test",
    "age": "23"
},
{
    "name": "Best", 
    "age": "24"
}]

and test.xml is just a regular file.而 test.xml 只是一个普通文件。

The curl request above looks like this:上面的 curl 请求如下所示:

{
  "method": "POST",
  "path": "/",
  "query": {},
  "headers": {
    "x-forwarded-for": "163.47.149.248",
    "x-forwarded-proto": "https",
    "x-forwarded-port": "443",
    "host": "mysimpleapi.com",
    "x-amzn-trace-id": "Root=1-618e4f8e-2cc109560ddfb65a7b5822ef",
    "content-length": "428",
    "user-agent": "curl/7.68.0",
    "accept": "*/*",
    "content-type": "multipart/mixed; boundary=------------------------e2d73e2082b30c29"
  },
  "bodyRaw": "--------------------------e2d73e2082b30c29\r\nContent-Disposition: attachment; name=\"request\"; filename=\"body.json\"\r\nContent-Type: application/json\r\n\r\n[{\n    \"name\": \"Test\",\n    \"age\": \"23\"\n},\n{\n    \"name\": \"Best\", \n    \"age\": \"24\"\n}]\n\r\n--------------------------e2d73e2082b30c29\r\nContent-Disposition: attachment; name=\"file1\"; filename=\"test.xml\"\r\nContent-Type: application/xml\r\n\r\n\r\n--------------------------e2d73e2082b30c29--\r\n",
  "body": "--------------------------e2d73e2082b30c29\r\nContent-Disposition: attachment; name=\"request\"; filename=\"body.json\"\r\nContent-Type: application/json\r\n\r\n[{\n    \"name\": \"Test\",\n    \"age\": \"23\"\n},\n{\n    \"name\": \"Best\", \n    \"age\": \"24\"\n}]\n\r\n--------------------------e2d73e2082b30c29\r\nContent-Disposition: attachment; name=\"file1\"; filename=\"test.xml\"\r\nContent-Type: application/xml\r\n\r\n\r\n--------------------------e2d73e2082b30c29--\r\n"
}

I want to get hold of the json object that's in the body of this request.我想获取此请求正文中的 json 对象。

I was not able to find any documentation on how to parse this type of request.我找不到有关如何解析此类请求的任何文档。 I know how to parse JSON and urlencoded form data from the request, but how do I parse a request like this on express?我知道如何从请求中解析 JSON 和 urlencoded 表单数据,但是如何在 express 上解析这样的请求? Is there any package that can do this parsing for me so that the request.body is not an empty object?是否有任何包可以为我进行解析,以便 request.body 不是空对象? Or I have to write some kind of parser myself(how do I do this?)?或者我必须自己编写某种解析器(我该怎么做?)?

Answer this myself, dicer for node js can be used to do exactly this.自己回答这个问题,可以使用 node js 的 dicer 来做到这一点。 The getting started example on dicer's npm worked just fine for the above example. dicer 的 npm 上的入门示例适用于上述示例。 https://www.npmjs.com/package/dicer https://www.npmjs.com/package/dicer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM