我試圖發送多個二進制文件到單個多部分/混合POST中的Web服務,但似乎無法讓它工作...目標服務器拒絕它。有一兩件事我注意到的是節點試圖做編碼爲分塊,這是我不想:非分塊multipart/mixed POST?
POST /SomeFiles HTTP/1.1
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=123456789
Host: host.server.com
Connection: keep-alive
Transfer-Encoding: chunked
我如何得到它停止被分塊? Docs表示可以通過在主請求頭中設置Content-Length來禁用它,但我無法設置Content-Length,因爲我一次加載一個文件 - 但它不應該因爲它是多部分的,所以需要它們。
我認爲其餘的是好的(除非它沒有分塊,除非req_post.write()
是做那部分),例如,初始第一部分:
var req_post = https.request({
hostname: 'host.server.com',
path: '/ManyFiles',
method: 'POST',
headers: {
'MIME-Version': '1.0',
'Content-Type': 'multipart/mixed; boundary=' + boundary
}
},...);
,然後僞代碼:
while (true) {
// { get next file here }
req_post.write('\r\n--' + boundary + '\r\nContent-Type: ' + filetype + '\r\nContent-Length: ' + filesize + '\r\n\r\n');
req_post.write(chunk);// filesize
if (end) {
req_post.write('\r\n--' + boundary + '--\r\n');
req_post.end();
break;
}
}
任何幫助/指針表示讚賞!
感謝您的回覆。我在每個MIME部分都包含了Content-Length,但即使沒有,MIME邊界也會標示消息的結束 - 也就是說,HTTP協議處理這種情況的確定。這似乎是Node的限制,而不是HTTP ... – mark
是的,這是正確的,但它需要知道整個響應何時被接收(即發送了多少個MIME部分)。這個wiki有一個很好的解釋。我建議至少閱讀第一部分。 http://en.wikipedia.org/wiki/Chunked_transfer_encoding。最終,如果你不想分塊編碼,你必須在整個請求體上計算和設置內容長度,而不僅僅是每個部分。 –