2014-05-15 69 views
0

我已經在Express.js中寫入了POST路由:/ api/file/upload。這條路線需要兩個參數才能工作 - 一個帶有發佈文件的「文件」參數和一個「apiKey」參數,它是一個字符串。爲了測試它,我想創造Fiddler2具有下列數據請求成功:帶有文件和字符串參數的HTTP POST請求到Node.js

頁眉:

Content-Type: multipart/form-data; boundary=-------------------------acebdf13572468 
User-Agent: Fiddler 
Host: localhost:8000 
Content-Length: 178037 

請求正文:

---------------------------acebdf13572468 
Content-Disposition: form-data; name="file"; filename="4Byl64P (1).jpg" 
Content-Type: image/jpeg 

<@INCLUDE *C:\Users\patrick\Pictures\4Byl64P (1).jpg*@> 
---------------------------acebdf13572468-- 


---------------------------acebdf13572468 
Content-Disposition: form-data; name="apiKey" 
Content-Type: application/json 

{ 
    "apiKey": "GKBG-QoNs-f74E-Z8Qn-zozm" 
} 
---------------------------acebdf13572468-- 

但是當我嘗試登錄參數在Node.js中,對於request.files,我得到一個空對象request.bodyundefined

如何使用Fiddler2將表單數據成功發佈到Node.js?

回答

1

您的身體畸形(過早結束邊界)。它應該看起來更像這樣:

---------------------------acebdf13572468 
Content-Disposition: form-data; name="apiKey" 
Content-Type: application/json 

{ 
    "apiKey": "GKBG-QoNs-f74E-Z8Qn-zozm" 
} 
---------------------------acebdf13572468 
Content-Disposition: form-data; name="file"; filename="4Byl64P (1).jpg" 
Content-Type: image/jpeg 

<@INCLUDE *C:\Users\patrick\Pictures\4Byl64P (1).jpg*@> 
---------------------------acebdf13572468--