【发布时间】:2017-09-28 22:30:20
【问题描述】:
我正在尝试使用表单数据在 https 请求中上传文件。我有这个
function uploadDocument(filepath) {
var options = {
host : 'myserver',
port : 443,
path : '/platform-api/v1/documents',
method : 'POST',
headers : {
'Authorization' : 'Bearer 56356363',
'Accept' : 'application/json'
}
};
var req = https.request(options, function(res) {
var buffer = "";
res.on('data', function(chunk) {
buffer += chunk;
});
res.on('end', function(chunk) {
var json = JSON.parse(buffer.toString());
console.log(json);
});
});
var form = new FormData();
form.append('file', fs.createReadStream(filepath));
form.append('project_id', 4);
form.pipe(req);
req.on('error', function(e) {
console.log('problem with request:', e.message);
});
req.end();
}
但我回来了
problem with request: write after end
{ errors:
{ file: 'missing-required-key',
project_id: 'missing-required-key' } }
有谁知道怎么回事?
谢谢
【问题讨论】:
-
我怎样才能像这里问的那样等待管道完成stackoverflow.com/questions/46479347/…
-
尝试删除
req.end()文档似乎是说没有它也可以工作
标签: javascript node.js http multipartform-data form-data