如何決議內容型別為 multipart/mixed in express 的請求正文?
此類請求的一個簡單示例如下:
curl -H "Content-Type: multipart/mixed" -F "[email protected];type=application/json" -F "[email protected]" -X POST https://mysimpleapi.com
Body.json 檔案是一個簡單的 json
[{
"name": "Test",
"age": "23"
},
{
"name": "Best",
"age": "24"
}]
而 test.xml 只是一個普通檔案。
上面的 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"
}
我想獲取此請求正文中的 json 物件。
我找不到有關如何決議此類請求的任何檔案。我知道如何從請求中決議 JSON 和 urlencoded 表單資料,但是如何在 express 上決議這樣的請求?是否有任何包可以為我進行決議,以便 request.body 不是空物件?或者我必須自己撰寫某種決議器(我該怎么做?)?
uj5u.com熱心網友回復:
自己回答這個問題,可以使用 node js 的 dicer 來做到這一點。dicer 的 npm 上的入門示例適用于上述示例。 https://www.npmjs.com/package/dicer
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/367139.html
標籤:javascript 节点.js 表达 解析
