4
我想用request-promise將文件上傳到服務器。節點JS請求 - 用驗證PUT的承諾
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') }
]
};
的constroller功能有:
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);
});
我從來沒有成功,也沒有捕獲錯誤。什麼一定是錯的?做實際的工作是這樣的捲曲:
curl --verbose -u myusername:mypassword -X PUT --upload-file test.jpg myurl.com/putImage/image.jpg
,這是什麼最好的方法?可以請求承諾嗎?