aef aef - 8 days ago 7
HTTP Question

No representation or empty representation in HTTP

In HTTP responses, is there a clear difference between a resource that has no representation and a resource that has a representation that is an empty string?

If so, how can I distinguish the two situations? Are there even multiple ways to express these?

Please provide references to the current IETF RFCs in your answers.

Answer

In HTTP responses, is there a clear difference between a resource that has no representation and a resource that has a representation that is an empty string?

HTTP has a specified way to describe a representation (a byte array) that is zero bytes long: 204 No Content

The 204 (No Content) status code indicates that the server has successfully fulfilled the request and that there is no additional content to send in the response payload body. Metadata in the response header fields refer to the target resource and its selected representation after the requested action was applied.

This is sometimes used in web apis to describe "flag" resources. The github api uses 204/404 to indicate the presence/absence of a star.

That said, your ability to distinguish a null representation from an empty representation is going to depend on the media type of that representation; client and server need to understand the message payload the same way, and they communicate that bit of meta-data via the Content-Type header.

For example, RFC 7231 specifies that the application/json representation is a JSON-text is a value (surrounded by optional white space), where valid productions of value include a string or a literal null. So those are easy to distinguish

On the other hand, if the content type is text/plain... well, a zero length byte array is the natural way to represent an empty text file, so I would expect an empty string to have the same representation. I have no idea at all what representation other than a zero length byte array would serve for "no representation", so I guess that case isn't distinguishable.

If I had to come up with a way to report zero length representations that are different, in some ineffable way... I suppose I'd encode the information in the content-type itself, taking media types from either the vendor tree or the personal tree. I'd feel vaguely slimy in doing so, and would worry about the implications on caches, conneg, and so forth.

It's not a problem I want to have.