0

Quoting https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#2xx_Success :

204 No Content

The server successfully processed the request and is not returning any content.

Is this part about not returning any content:

  • Enforced by the HTTP protocol, so that it can be trusted that if we're getting 204 then the response surely and certainly has an empty body?
  • Required by the HTTP specification and widely obeyed but not made impossible; so in practice, while a server theoretically can send a body with a code of 204, in practice it can be still assumed not to happen and if this happens then the server is broken?
  • In theory required by the HTTP specification or at least strongly recommended; but in practice this cannot be relied upon as there are servers that break this assumption - in a similar way a web browser cannot assume the website it tries to fetch most certainly contains valid HTML and must be prepared to handle the opposite situation?

Or, in other words: If I'm getting an HTTP 204 response code, can I safely ignore the possibility of getting a non-empty body without fear of loss of data? Or do best practices dictate that I should check that the body is indeed empty and if it's not then I should not silently discard the returned data?

INDRAJITH EKANAYAKE
  • 3,894
  • 11
  • 41
  • 63
  • "Enforced by the HTTP protocol" - a protocol is not a machine but rather a common understanding of behaviour. A baking recipe for gluten-free cakes does not enforce any cake baked according to the recipe to be actually gluten-free. Enforcing has to be done by the parties trying to communicate via the protocol. – Smutje May 08 '19 at 13:46

2 Answers2

0

There's only your client and the server. The server can be anything, any custom piece of software. HTTP is just text. It's entirely possible that some HTTP server outputs a 204 status code followed by a response body. There's nothing in between that server and you that would prevent that from happening.

So, yes, it's possible. It would be nonsensical. It would be entirely correct of you, the client, to even stop reading from the socket after you have received a 204 code and entirely discard the body. Whether you want to do that, or you want to expect possible body data after a 204 is up to you. Specification-wise "204" means "no body data".

deceze
  • 510,633
  • 85
  • 743
  • 889
  • Nope. It *is* enforced by the protocol. Any data received would be the subsequent response. Yes, a server can misbehave, but by *definition*, there is no response body. – Julian Reschke May 08 '19 at 19:19
  • *Who* enforces it‽ If I program my server to output 204 *and* a body, you will receive that. – deceze May 08 '19 at 19:40
  • Yes. But if the recipient is written to the spec, it will not process it as the body. If it does, it's broken. – Julian Reschke May 08 '19 at 20:29
0

https://greenbytes.de/tech/webdav/rfc7230.html#message.body.length:

The length of a message body is determined by one of the following (in order of precedence):

  1. Any response to a HEAD request and any response with a 1xx (Informational), 204 (No Content), or 304 (Not Modified) status code is always terminated by the first empty line after the header fields, regardless of the header fields present in the message, and thus cannot contain a message body.
Community
  • 1
  • 1
Julian Reschke
  • 40,156
  • 8
  • 95
  • 98