2016-07-19 4 views
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 

これにはどのような方法が最適ですか?このことを要求することができますか?

答えて

4

は、それがオプションに変更してアップロードするガット:

var options = { 
     uri: 'myurl.com/putImage/image.jpg', 
     method: 'PUT', 
     auth: { 
     'user': 'myusername', 
     'pass': 'mypassword' 
     }, 
     multipart: [ 
     { body: fs.createReadStream('test.jpg') } 
     ] 
    }; 

は、任意のより良い方法はありますか?

関連する問題