简体   繁体   English

NodeJS - 向远程主机发出 HTTP 请求时抛出 ERR_INVALID_ARG_TYPE 错误

[英]NodeJS - ERR_INVALID_ARG_TYPE Error thrown while issuing a HTTP request to remote host

Recently, I encountered a problem while trying to issue a request using NodeJS and request-promise .最近,我在尝试使用 NodeJS 和request-promise发出请求时遇到了一个问题。

The following code is nested inside a multer call for file uploading (using nested functions / clusters.以下代码嵌套在用于文件上传的 multer 调用中(使用嵌套函数/集群。

    const options = {
        method: 'POST',
        uri: 'URL of your choice',
        body: {
            //Body of the request
        },
        // json: true,
        headers: {
            // 'Content-Type': 'application/x-www-form-urlencoded',
        },
    }

    request(options)
        .then(function (response) {
            console.log('Response: ', response);

        })
        .catch(function (err) {
            console.log('Error: ', err);
        });

While using the current request, without the 'json: true' property (commented out), I get the following error:在使用当前请求时,如果没有 'json: true' 属性(注释掉),我收到以下错误:

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be one of type string or Buffer. Received type object
    at write_ (_http_outgoing.js:603:11)
    at ClientRequest.write (_http_outgoing.js:575:10)
    at Request.write (PATH/node_modules/request/request.js:1500:27)
    at end (PATH/node_modules/request/request.js:549:18)
    at Immediate.<anonymous> (PATH/node_modules/request/request.js:578:7)
    at runCallback (timers.js:696:18)
    at tryOnImmediate (timers.js:667:5)
    at processImmediate (timers.js:649:5)
    at process.topLevelDomainCallback (domain.js:121:23)

And when I turn the 'json: true' option on, the problem doesn't occur, but the remote API returns an error as it doesn't handle JSON requests/their added curly braces well.当我打开 'json: true' 选项时,问题不会发生,但远程 API 返回错误,因为它不能很好地处理 JSON 请求/他们添加的花括号。

Any ideas about getting over this issue?关于克服这个问题的任何想法?

Thank you.谢谢你。

Solved it!解决了!
As the remote host doesn't handle JSON well, and required "ordinary" POST request to be sent, I looked again inside request-promise's documentation.由于远程主机不能很好地处理 JSON,并且需要发送“普通”POST 请求,我再次查看了 request-promise 的文档。 By changing body{} to formData{} , and commenting out json: true , the problem was solved.通过将body{}更改为formData{} ,并注释掉json: true ,问题就解决了。

const options = {
    method: 'POST',
    uri: 'URL of your choice',
    formData: {
        //Request's data
    },
}

request(options)
    .then(function (response) {
        console.log('Response: ', response);

    })
    .catch(function (err) {
        console.log('Error: ', err);
    });

Try Below -试试下面 -

url = "your url"
const options = {
  url: url,
  method: 'POST',
  headers: {
    Accept: 'application/json',
    'Accept-Charset': 'utf-8'
  },

  body: {

  }
};
request.post(options, function (err, response, body) {
      // do something with your data
console.log(response.statusCode);
console.log(body)
});

我遇到了类似的问题,就我而言,上传目录未正确定义,请确保您要上传文件的路径存在且已明确定义

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试登录时出现错误 [ERR_INVALID_ARG_TYPE] - Getting error [ERR_INVALID_ARG_TYPE] while trying to login 抛出新的 ERR_INVALID_ARG_TYPE('第一个参数',nodeJS - throw new ERR_INVALID_ARG_TYPE('first argument', nodeJS nodejs - TypeError [ERR_INVALID_ARG_TYPE]:第一个参数必须是字符串类型之一,缓冲区 - 使用 http.request 的回调响应时 - nodejs - TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be one of type string, Buffer - when using callback response with http.request 服务器错误消息 [ERR_INVALID_ARG_TYPE]: - server error message [ERR_INVALID_ARG_TYPE]: 尝试通过 Typeorm 将值插入 Mssql 数据库时出现“ERR_INVALID_ARG_TYPE”错误 - Getting "ERR_INVALID_ARG_TYPE" error while trying to insert values in to Mssql database through Typeorm 类型错误 [ERR_INVALID_ARG_TYPE] - TypeError [ERR_INVALID_ARG_TYPE] 在 node.js 中使用 request-promise 模块并获取 http_outgoing.js:618 throw new ERR_INVALID_ARG_TYPE(&#39;first argument&#39;, error - post using request-promise module in node.js and getting http_outgoing.js:618 throw new ERR_INVALID_ARG_TYPE('first argument', error TypeError [ERR_INVALID_ARG_TYPE] 尝试将错误写入 Nodejs 中的文件时 - TypeError [ERR_INVALID_ARG_TYPE] When trying to write err to file in Nodejs NodeJS - TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。 收到未定义 - NodeJS - TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined 为什么s3.putObject为nodejs Buffer抛出ERR_INVALID_ARG_TYPE? - Why does s3.putObject throw ERR_INVALID_ARG_TYPE for nodejs Buffer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM