具有auth的PUT的节点JS请求承诺

时间:2022-01-28 16:55:05

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:

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);
  });

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?

对此最好的方法是什么?可以请求 - 承诺这样做吗?

1 个解决方案

#1


4  

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?

有没有更好的方法?

#1


4  

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?

有没有更好的方法?