我想通过请求 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') }
]
};
Controller 函数有:
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 :
curl --verbose -u myusername:mypassword -X PUT --upload-file test.jpg myurl.com/putImage/image.jpg
最好的方法是什么? request-promise 可以做到这一点吗?
最佳答案
更改选项后上传:
var options = {
uri: 'myurl.com/putImage/image.jpg',
method: 'PUT',
auth: {
'user': 'myusername',
'pass': 'mypassword'
},
multipart: [
{ body: fs.createReadStream('test.jpg') }
]
};
有没有更好的方法?
关于node.js - PUT 的 Node JS 请求 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38455043/