request: first argument must be a string of Buffer

Hi, I was running some code and got this error message:

_http_outgoing.js:441
    throw new TypeError('first argument must be a string or Buffer');
    ^

TypeError: first argument must be a string or Buffer
    at ClientRequest.OutgoingMessage.write (_http_outgoing.js:441:11)
    at Request.write (/Users/tomasnovella/workspace/kitt/kitt-chrome-sync/node_modules/request/request.js:1392:25)
    at end (/Users/tomasnovella/workspace/kitt/kitt-chrome-sync/node_modules/request/request.js:550:16)
    at Immediate._onImmediate (/Users/tomasnovella/workspace/kitt/kitt-chrome-sync/node_modules/request/request.js:578:7)
    at processImmediate [as _immediateCallback] (timers.js:383:17)

Funny thing is, that if I replace the current version of request module with an older one(### v2.60.0 (2015/07/21), according to changelog), everything works just fine.

The code that causes this error is this:

  request.post({
        url: '<someURL>',
        qs: {
          'client': 'Google+Chrome',
          'client_id': config.clientId
        },
        headers: {
          'Content-Type': 'application/octet-stream',
          'Authorization': 'Bearer '+ accessToken
        },
        encoding: null, //  if you expect binary data
        responseType: 'buffer', 
        body: body
    }, (error, response, body) => {..blah...});

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 8
  • Comments: 26 (9 by maintainers)

Most upvoted comments

Basically do not use body: {} in your request. If you want to pass data in the body, use json: {} instead

Set the json option to true, also take a look at the options section in the docs. Your last question is not related to this issue, post a new one if you have other questions.

So, basically the body should be either String or Buffer, not sure about these new types.

@banomaster @Ohar body must be String or Buffer

Wow, bravo request team, way to break the interface without upgrading major versions 😕

Hey everyone, I have been fiddling with this error for a while, and here is what I found out. When the post request params is of type: {form: {key: 1}} it automatically serializes the post parameters, but when it is something else, say {body: {key: 1}} it throws the new TypeError('first argument must be a string or Buffer'); error.

To overcome this, I just did {body: JSON.stringify({key: 1})} and the request succeeded. I haven’t looked into the source yet, but this worked for me.

Also, if we set json: true in the options object, it automatically serializes the request params.

Hey guys,

i have version 2.70.0 installed, but I am still struggling with this same error:

TypeError: first argument must be a string or Buffer
    at ServerResponse.OutgoingMessage.end (_http_outgoing.js:524:11)
    at /Users/urbanmarovt/Documents/Faks/BCH/Diploma/PoC/gateway/routes/index.js:87:17
    at Request._callback (/Users/urbanmarovt/Documents/Faks/BCH/Diploma/PoC/gateway/routes/proxy.js:81:7)
    at Request.self.callback (/Users/urbanmarovt/Documents/Faks/BCH/Diploma/PoC/gateway/node_modules/request/request.js:200:22)

This is my code that I currently use:

var options = {
      url: url,
      path: req.path,
      qs: req.query,
      method: req.method,
      headers: headers,
      responseType: 'buffer',
      timeout: TIMEOUT
    };

 options.json = true;
 options.body = {
        "firstName": "Test1",
        "lastName": "Test2",
        "email": "test@gmail.com",
        "mobilePhone": "+38631288288"
 };

 request(options, function (error, response, body) {
      callback(error, response, body, options);
    });

I hope my code and question are clear enough.

Hey, just came across with a solution to this. I was sending a json body, but I needed to convert it to string with body: JSON.stringify(body)