我正在尝试通过请求npm库发送文件,但我一直收到write after end
错误。怎么了?我该如何解决这个问题?
这就是我这样做的方式:
return new Promise((resolve, reject) => {
const callback = (error, response, body) => {
if (!error && response.statusCode < 400) {
resolve(body);
}
else {
reject(error); // Write after end
}
};
request({
url: "https://api.url.com/v2/",
method: "POST",
formData: fs.createReadStream("file"),
}, callback);
});
答案 0 :(得分:0)
我认为发送数据的方法是将表单数据附加到请求中:
var req = request.post(url, callback);
var form = req.form();
form.append('file', fs.createReadStream(filepath));