【问题标题】:Nodejs request-promise error when sending POST request?发送POST请求时Nodejs请求承诺错误?
【发布时间】:2018-12-28 17:20:33
【问题描述】:

当我尝试使用通过 npm 安装的 request-promise 模块发送发布请求时,我收到一个错误,我不知道为什么。

我曾尝试删除部分选项和标题,但直截了当地说,我只是卡住了。

    function sendData() {
        var options = {
            method: 'POST',
            uri: `http://www.myurl.com/placeholder.json`,
            body: {
                "data":"desiredData"
            },
            headers: {
                "Accept": "application/json",
                "Accept-Encoding": "gzip, deflate",
                "Accept-Language": "en-US,en;q=0.9",
                "Connection": "keep-alive",
                "Content-Type": "application/x-www-form-urlencoded",
                "X-Requested-With": "XMLHttpRequest"
            }
        }
        rp(options)
            .then(function (parsedBody) {
                console.log(parsedBody)
            })
            .catch(function (err) {
                console.log(err)
            });
    }

sendData();

应该发送一个发布请求并记录返回的 json,但我收到以下错误:

_http_outgoing.js:654
    throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'first argument',
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be one of type string or Buffer
    at write_ (_http_outgoing.js:654:11)
    at ClientRequest.write (_http_outgoing.js:629:10)
    at Request.write (C:\Users\ninja_000\Desktop\TronJS\node_modules\request\request.js:1500:27)
    at end (C:\Users\ninja_000\Desktop\TronJS\node_modules\request\request.js:549:18)
    at Immediate.<anonymous> (C:\Users\ninja_000\Desktop\TronJS\node_modules\request\request.js:578:7)
    at runCallback (timers.js:756:18)
    at tryOnImmediate (timers.js:717:5)
    at processImmediate [as _immediateCallback] (timers.js:697:5)

【问题讨论】:

  • 如果我的回答对您有用,您可以接受或限定它,以便其他有类似问题的用户可以更快地找到解决方案。

标签: node.js request


【解决方案1】:

我创建了这个示例以便理解request-promise 的功能。

option.body 设置为您的数据并设置json: true 将正文编码为JSON。

我使用了一个简单的在线假 REST API 服务器,因此您无需更改任何内容即可运行示例。

const rp = require('request-promise');
function sendData() {
  var options = {
    method: 'POST',
    uri: 'https://jsonplaceholder.typicode.com/posts',
    body: {
      title: 'foo',
      body: 'bar',
      userId: 1
    },
    headers: {
      "Content-type": "application/json; charset=UTF-8"
    },
    json: true // Automatically stringifies the body to JSON
  };

  rp(options)
    .then((parsedBody) => {
      console.log(parsedBody);
    })
    .catch((err) => {
      console.log(err);
    });
}
sendData();

【讨论】:

  • 我现在收到了,
  • \node_modules\request\lib\querystring.js:43 return str.replace(/[!'()*]/g, function (c) { ^ TypeError: str.replace is not a Querystring.rfc3986 处的函数(C:\Users\ninja_000\Desktop\TronJS\node_modules\request\lib\querystring.js:43:14)
  • 在 Request.json (C:\Users\ninja_000\Desktop\TronJS\node_modules\request\request.js:1293:30) 在 Request.init (C:\Users\ninja_000\Desktop\ TronJS\node_modules\request\request.js:406:10) 在 Request.RP$initInterceptor [as init] (C:\Users\ninja_000\Desktop\TronJS\node_modules\request-promise-core\configure\request2.js: 45:29) 在新请求 (C:\Users\ninja_000\Desktop\TronJS\node_modules\request\request.js:127:8) 在请求
  • (C:\Users\ninja_000\Desktop\TronJS\node_modules\request\index.js:53:10) 在 Timeout.ATC [as _onTimeout] (C:\Users\ninja_000\Desktop\ TronJS\tron.js:112:5) 在 ontimeout (timers.js:458:11) 在 tryOnTimeout (timers.js:296:5) 在 Timer.listOnTimeout (timers.js:259:5)
  • 该错误是因为您没有正确遵循示例,您正在更改请求的正文。 @奥格登
【解决方案2】:

在错误消息中它表示参数错误,因此请检查 sendData() 函数中代码的正文部分。

试试这个,

function sendData() {
    var options = {
        method: 'POST',
        uri: 'http://www.myurl.com/placeholder.json',
        body: {
            data:'desiredData'
        },
        headers: {
            "Accept": "application/json",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "en-US,en;q=0.9",
            "Connection": "keep-alive",
            "Content-Type": "application/x-www-form-urlencoded",
            "X-Requested-With": "XMLHttpRequest"
        }
    }
    rp(options)
        .then(function (parsedBody) {
            console.log(parsedBody)
        })
        .catch(function (err) {
            console.log(err)
        });
}

我编辑了你的代码的第 6 行。

【讨论】:

    【解决方案3】:

    我设法修复它,起初我在标题下添加了json: true,然后导致上面发布的另一个错误。为了解决这个问题,我还必须将内容类型更改为 application/json,感谢所有提供帮助的人。

    【讨论】:

    • 这些建议在我的回复中,您可以接受或符合条件。
    【解决方案4】:

    data:'desiredData' 应该是 data:desiredData 其中desiredData 是一个 json 对象。例如:

    var desiredData = { propertyName: propertyValue } 
    

    【讨论】:

      【解决方案5】:

      尝试更改内容类型

      function sendData() {
          var options = {
              method: 'POST',
              uri: `http://www.myurl.com/placeholder.json`,
              body: {
                  "data":"desiredData" // this should be a JSON format if you are using 
                                      //application/json
              },
              headers: {
                  "Accept": "application/json",
                  "Accept-Encoding": "gzip, deflate",
                  "Accept-Language": "en-US,en;q=0.9",
                  "Connection": "keep-alive",
                  "Content-Type": "application/json",
                  "X-Requested-With": "XMLHttpRequest"
              }
      }
      
      rp(options)
          .then(function (parsedBody) {
               console.log(parsedBody)
          })
          .catch(function (err) {
               console.log(err)
          });
      }
      

      【讨论】:

      • 似乎没有帮助
      • 尝试删除整个标题元详细信息。让它选择默认值,然后重试
      猜你喜欢
      • 1970-01-01
      • 2021-01-14
      • 1970-01-01
      • 2018-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多