Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept empty responses on json response type #375

Open
szmarczak opened this issue Aug 27, 2021 Discussed in #374 · 3 comments
Open

Accept empty responses on json response type #375

szmarczak opened this issue Aug 27, 2021 Discussed in #374 · 3 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@szmarczak
Copy link
Collaborator

szmarczak commented Aug 27, 2021

Got: https://github.com/sindresorhus/got/blob/295979693bb179b32d11107367c57c31cb05bf78/source/core/response.ts#L140-L142

Discussed in #374

Originally posted by cruzms August 27, 2021

Hi i'm getting SyntaxError: JSON Parse error: Unexpected EOF, after receiving an empty response from server with a status 200.
The only way to changing this behavior is changing responses from server to no content(204) ?
Loggin my response with afterResponse hook i got this

{
  "type":"default",
  "status":200,
  "ok":true,
  "statusText":"",
  "headers":{
    "map":{
      "content-length":"0",
      "connection":"keep-alive",
      "date":"Thu, 26 Aug 2021 22:37:33 GMT",
      "keep-alive":"timeout=5"
    }
  },
  "url":"http://localhost/test",
  "bodyUsed":false,
  "_bodyInit":{
    "_data":{
      "size":0,
      "offset":0,
      "blobId":"c4a5f299-c8f6-46bf-84e3-4ed7a981b5b2",
      "__collector":{
      }
    }
  },
  "_bodyBlob":{
    "_data":{
      "size":0,
      "offset":0,
      "blobId":"c4a5f299-c8f6-46bf-84e3-4ed7a981b5b2",
      "__collector":{
      }
    }
  }
}
```</div>
@szmarczak szmarczak added enhancement New feature or request help wanted Extra attention is needed labels Aug 27, 2021
@matei-radu
Copy link

matei-radu commented Aug 28, 2021

According to the HTTP/1.1 specifications (RFC 7231), 200 OK responses have a payload body, while 204 No Content don't. With this in mind, I believe ky is behaving correctly.

However, I can also understand that many APIs and developers do not respect this distinction (or maybe they're lazy :P) so, spec complaint or not, maybe we need this feature anyway.

@szmarczak
Copy link
Collaborator Author

szmarczak commented Aug 28, 2021

You missed this part:

an origin server MAY generate a payload body of zero length.
If no payload is desired, an origin server ought to send 204

ought to =/= must

@matei-radu
Copy link

I stand corrected, thanks for digging that up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants