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)
Basically do not use
body: {}in your request. If you want to pass data in the body, usejson: {}insteadSet the
jsonoption totrue, 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
StringorBuffer, not sure about these new types.@banomaster @Ohar
bodymust beStringorBufferWow, 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 thenew 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: truein 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:
This is my code that I currently use:
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 withbody: JSON.stringify(body)