Skip to content Skip to sidebar Skip to footer

How Do I Send A Form With A Request Promise?

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 modu

Solution 1:

According 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!');
});

Post a Comment for "How Do I Send A Form With A Request Promise?"