I want to upload a file to server with request-promise.
var requestPromise = require('request-promise');
var options = {
uri: 'myurl.com/putImage/image.jpg',
method: 'PUT',
auth: {
'user': 'myusername',
'pass': 'mypassword',
'sendImmediately': false
},
multipart: [
{
'content-type': 'application/json',
body: JSON.stringify({foo: 'bar', _attachments: {'message.txt': {follows: true, length: 18, 'content_type': 'text/plain' }}})
},
{ body: 'I am an attachment' },
{ body: fs.createReadStream('test.jpg') }
]
};
The constroller function has:
requestPromise.put(options)
.then(function() {
response = {
statusCode: 200,
message: "Success",
};
log.info("Success");
res.status(200).send(response);
})
.catch(function (error) {
response = {
statusCode: error.status,
message: error.message
};
log.info(response);
res.status(400).send(response);
});
I never get to success nor catch an error. What must be wrong? Actual work to be done is a curl like this:
curl --verbose -u myusername:mypassword -X PUT --upload-file test.jpg myurl.com/putImage/image.jpg
What is the best approach for this? Can request-promise do this?
This package is also deprecated because it depends on request . Fyi, here is the reasoning of request 's deprecation and a list of alternative libraries.
In Node. js, we can use the util. promisify() utility module to easily transform a standard function that receives a callback into a function that returns a promise.
Got it to upload with change in options:
var options = {
uri: 'myurl.com/putImage/image.jpg',
method: 'PUT',
auth: {
'user': 'myusername',
'pass': 'mypassword'
},
multipart: [
{ body: fs.createReadStream('test.jpg') }
]
};
Is there any better approach?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With