【问题标题】:Error: Argument error, options.body with node.js request library错误:参数错误,带有 node.js 请求库的 options.body
【发布时间】:2018-12-21 21:50:53
【问题描述】:

我正在尝试使用 request library 从多台(最多两台)打印机进行打印:

for(var i = 0; i < printers.length; i++) {
  var body = {
    "printerid": printers[i], 
    "ticket": JSON.stringify(ticket),
    "title": "TEST PRINT",
    "content": content,
    "contentType": "text/plain"
  };
  request({
    "method": "POST",
    "url": googlePrintUrl+"submit",
    "headers": {
      "Authorization": "OAuth " + googleAccessToken
    },
    "body": body
  }, function (error, res, body){
    if (error) {
      console.log("There was an error with Google Cloud Print");
      console.log(error);
      return;
    }
    console.log("The server responded with:", body);
  }); 
} 

我收到此错误:

Error: Argument error, options.body.
    at setContentLength (/user_code/node_modules/request/request.js:433:28)
    at Request.init (/user_code/node_modules/request/request.js:438:5)
    at new Request (/user_code/node_modules/request/request.js:127:8)
    at request (/user_code/node_modules/request/index.js:53:10)
    at printByGoogleCloud (/user_code/index.js:211:5)
    at admin.database.ref.once.then.then.then (/user_code/index.js:380:13)

TypeError: First argument must be a string or Buffer
    at ClientRequest.OutgoingMessage.write (_http_outgoing.js:457:11)
    at Request.write (/user_code/node_modules/request/request.js:1495:27)
    at end (/user_code/node_modules/request/request.js:545:18)
    at Immediate.<anonymous> (/user_code/node_modules/request/request.js:574:7)
    at runCallback (timers.js:672:20)
    at tryOnImmediate (timers.js:645:5)
    at processImmediate [as _immediateCallback] (timers.js:617:5)

这似乎是我们使用 request 库的方式的错误,而不是 api。但我无法确切地弄清楚错误是什么。我对请求进行了许多不同的更改,但都没有奏效。感谢您的帮助。

【问题讨论】:

    标签: node.js request


    【解决方案1】:

    对于您的实际发布数据,请尝试使用 JSON.stringify。

    "body" : JSON.stringify(body)
    

    见:Request JSON Payload

    或者你可以传递 json 而不是 body。

    json: body
    

    见:JSON HTTP POST Request

    【讨论】:

    • 应该被标记为答案,至少它解决了我的问题。非常感谢您
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-24
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 2017-09-12
    • 2019-01-24
    • 1970-01-01
    相关资源
    最近更新 更多