admin管理员组文章数量:1356077
I am trying to use the request-promise library or anything similar to send files via a post request from node to another machine that is running Node. Using the normal request module I could so something like
var req = request.post(url, function (err, resp, body) {
if (err) {
console.log('Error!');
} else {
console.log('URL: ' + body);
}
});
var form = req.form();
form.append('file', '<FILE_DATA>', {
filename: 'myfile.txt',
contentType: 'text/plain'
});
This code is from the question: Uploading file using POST request in Node.js however it is not using promises.
Can anyone explain how to do the same thing but with the request-promise library or if there is any other way to promisify this?
I am trying to use the request-promise library or anything similar to send files via a post request from node to another machine that is running Node. Using the normal request module I could so something like
var req = request.post(url, function (err, resp, body) {
if (err) {
console.log('Error!');
} else {
console.log('URL: ' + body);
}
});
var form = req.form();
form.append('file', '<FILE_DATA>', {
filename: 'myfile.txt',
contentType: 'text/plain'
});
This code is from the question: Uploading file using POST request in Node.js however it is not using promises.
Can anyone explain how to do the same thing but with the request-promise library or if there is any other way to promisify this?
Share Improve this question edited May 23, 2017 at 10:28 CommunityBot 11 silver badge asked Jun 14, 2016 at 13:28 nbroekingnbroeking 8,9485 gold badges24 silver badges43 bronze badges 2-
you want
req
to return a promise? – maioman Commented Jun 14, 2016 at 13:36 -
What is purpose of using
Promise
? – guest271314 Commented Jun 14, 2016 at 13:36
1 Answer
Reset to default 6According to the docs that are linked from the answer you already found, you don't need to use a .form()
method on the resulting request object, but can simply pass the form as the formData
option to request
. You'll be able to do the same with request-promise:
requestPromise.post({url: url, formData: {
file: {
value: '<FILE_DATA>',
options: {
filename: 'myfile.txt',
contentType: 'text/plain'
}
}
}).then(function(body) {
console.log('URL: ' + body);
}, function(err) {
console.log('Error!');
});
Alternatively, request-promise still seems to return request instances (just decorated with then
/catch
/promise
methods), so the form
function should still be available:
var req = requestPromise.post(url);
var form = req.form();
form.append('file', '<FILE_DATA>', {
filename: 'myfile.txt',
contentType: 'text/plain'
});
req.then(function(body) {
console.log('URL: ' + body);
}, function(err) {
console.log('Error!');
});
本文标签: javascriptHow do I send a form with a request promiseStack Overflow
版权声明:本文标题:javascript - How do I send a form with a request promise? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744046696a2581656.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论