mikeal / bent

Functional JS HTTP client (Node.js & Fetch) w/ async await

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error when POSTing JSON and response is blank

PikaDude opened this issue · comments

If you post JSON to a website that returns an empty response, the following error will be thrown:

SyntaxError: Unexpected end of JSON inputstr""
    at JSON.parse (<anonymous>)
    at ClientRequest.<anonymous> (node_modules\bent\src\nodejs.js:109:24)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

The returned response is completely empty and the status code is 202. 202 has been added as an acceptable status code for the request.

A few questions.

  • Are you passing in the ‘json’ string as an option? If so, try it without, as that option is specifically meant to tell bent to parse the response body as JSON.
  • Does the response body contain an application/json header even though there’s no body?
  • Did you include 202 as an option to bent? If not, even once this error is gone you’ll get a new error since by default the only acceptable response code is 200.
  • I am passing the json string as an option as I thought it was needed to indicate that I was posting JSON, which I am. I will test this when I get the chance and tell you the results.
  • No, it does not.
  • I did include 202 as an option to bent as already stated.

Ya, this should work once you remove ’json’ from the options. That option is strictly for decoding the response body, we’re actually quite aggressive about encoding the POST body as json and don’t require any option to be set, it happens if you pass any object that isn’t a buffer https://github.com/mikeal/bent/blob/master/src/nodejs.js#L131

Yup! Removing json from the options solved the issue. Everything now works as intended. Thanks!