node.js - PUT 的 Node JS 请求 promise

标签 node.js curl request promise

我想通过请求 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/

相关文章:

node.js - forEach 中的 Puppeteer 测试

javascript - 请求模块 node.js : How add query to GET request?

javascript - file[i] === 10 是什么意思?

node.js - nodejs ejs 包含意外的标识符

python - 将 cURL 请求转换为 Python-Requests 请求

ruby - 如何将curl 的输出读入ruby 变量?

javascript - 使用 Linux/Windows CLI 行工具模拟 JavaScript 按钮单击(http 请求)

http - 传入请求的上下文

xml - 经典 asp 中的 System.Net.HttpWebRequest?

node.js - typescript 错误: TS2339:Property 'map' does not exist on type 'string'