Using [email protected]:
I'm looking to post raw content, so that I set the body as a string and receive json. However, setting body to a string and json to true results in the error:
TypeError: options.body must be a plain Object when options.form or options.json is used
Behaves as expected in v6.7.1
The json option is for POST'ing and receiving JSON. Why is your body a string?
My payload is signed, so I'd like to take care of the serialization myself.
@abrkn I'd like to point you to the following line in our documentation for the json option.
body must be a plain object and will be stringified.
If that isn't what you want the json option should be set to false 馃槈.
@sindresorhus .. thank you for the great work on this lib
I can't figure this case -
got('url/u', {json: true, method: 'POST', body: {asd: 1}}).then(console.log)
all works good, but some docker api endpoints return just lines of text, not json. So I get this error:
ParseError: Unexpected token C in JSON at position 0 in "http://unix/var/run/docker.sock:/exec/b37de4c64a65105cac691b176101ac42de4e10ccbd28cb50e207d34d161d5f37/start": ...
May you please guide how to use this lib in such case? In order to disable automatic JSON.parse
UPDTE: nvm, the solution:
headers = {'accept': 'application/json','content-type': 'application/json',}
got('url/u', {json: !true, method: 'POST', body: JSON.stringify({asd: 1}), headers}).then(console.log)
and json parse the response as/if needed
See https://github.com/sindresorhus/got/issues/467#issuecomment-403189894 for discussion and decision on the behavior of the jsonoption.
@talbergs solution above is the recommended approach if you need to support sending a JSON body while receiving a non-JSON response:
```js
const headers = {
'content-type': 'application/json'
}
got('example.com', {
json: false,
method: 'POST',
body: JSON.stringify({your: 'data'}),
headers
})
``
Most helpful comment
My payload is signed, so I'd like to take care of the serialization myself.