0

I'm trying to print from multiple (up to two) printers using the 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);
  }); 
} 

I'm getting this error:

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)

and

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)

This seems to be an error with the way we are using the request library, not with the api. But I can't figure out exactly what the error is. I've tried many different changes to the request, but none have worked. Thankful for help.

hong4rc
  • 3,999
  • 4
  • 21
  • 40
Sahand
  • 7,980
  • 23
  • 69
  • 137

1 Answers1

3

For your actual post data, try using JSON.stringify.

"body" : JSON.stringify(body)

See: Request JSON Payload

Or you can pass json instead of the body.

json: body

See: JSON HTTP POST Request

basic
  • 3,348
  • 3
  • 21
  • 36