有个需求需要在 nodejs 后端发送 multipart/form-data 请求做文件上传。
有个初步想法就是: 自己拼接表单到 body
大概这样:
request.write( '--' + boundaryKey + '\r\n'
'Content-Type: application/octet-stream\r\n'
'Content-Disposition: form-data; name="my_file"; filename="my_file.bin"\r\n'
'Content-Transfer-Encoding: binary\r\n\r\n' ); fs.createReadStream('./my_file.bin', { bufferSize: 4 * 1024 }) .on('end', function() {
request.end('\r\n--' + boundaryKey + '--'); }) // set "end" to false in the options so .end() isn't called on the request .pipe(request, { end: false })