Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP status codes : 204 OR 200 with body containing an empty object?

Tags:

http

web

rfc2616

I'm having trouble to understand the use cases of the 204 HTTP status code. The RFC2616 says :

10.2.5 204 No Content

The server has fulfilled the request but does not need to return an entity-body, and might want to return updated metainformation. The
response MAY include new or updated metainformation in the form of
entity-headers, which if present SHOULD be associated with the
requested variant.

If the client is a user agent, it SHOULD NOT change its document view from that which caused the request to be sent. This response is primarily intended to allow input for actions to take place without causing a change to the user agent's active document view, although any new or updated metainformation SHOULD be applied to the document currently in the user agent's active view.

The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields.

Does the "document view" refers to the DOM ?

If for instance, I fire an AJAX request deleting a user and I update my page to remove the user from a list once my request is successfully completed , should the server give me a 200 with a {} as a response or a 204 without a body ?

Edit : My main concern is related to the "If the client is a user agent, it SHOULD NOT change its document view from that which caused the request to be sent." part. To reformulate it with my own words : Can I update the DOM if I return a 204 ?

like image 852
Alexandre Nucera Avatar asked Jun 11 '14 08:06

Alexandre Nucera


People also ask

Can a 204 response have a body?

The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields.

How do I send 204 without content?

By default, 204 (No Content) the response is cacheable. If caching needs to be overridden then the response must include cache respective cache headers. For example, you may want to return status 204 (No Content) in UPDATE operations where request payload is large enough not to transport back and forth.

What is the meaning of status code 204?

The HTTP 204 No Content success status response code indicates that a request has succeeded, but that the client doesn't need to navigate away from its current page.

Can HTTP response have empty body?

1. Any response message which "MUST NOT" include a message-body (such as the 1xx, 204, and 304 responses and any response to a HEAD request) is always terminated by the first empty line after the header fields, regardless of the entity-header fields present in the message.


1 Answers

Both are actually valid. It all depends of your application.

A valid deletion can return both 200 or 204 response code.

The document view does refer to the DOM, yes.

This question talks about it.

like image 63
Thomas Ruiz Avatar answered Sep 18 '22 18:09

Thomas Ruiz