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

2

23

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 ?

Rearward answered 11/6, 2014 at 8:6 Comment(1)
You really ought to read the current RFC which has updated prose on this, see svn.tools.ietf.org/svn/wg/httpbis/specs/rfc7231.html#status.204Paratrooper
A
5

Does the "document view" refers to the DOM ?

Not exactly, it means the rendered document. DOM is not a representation of the rendered document. It is is abstract model of the documents tree. Each UA may render it differently.

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?

This depends on the API and how 'conformant' it is. So, if you submitted a delete request using the 'DELETE' verb (so, its a RESTful API), then the response should be 200 OK; but it can also send a 204 if the API is especially pedantic.

If you are just sending a GET request to '/some/view?id=1&action=delete', it will most likely return 200 OK

In either case, you are free to change the DOM (and thus, trigger a re-rendering of the document) because of the second part:

although any new or updated metainformation SHOULD be applied to the document currently in the user agent's active view.

So, using <strike> to indicate that the record has been deleted would conform to this requirement.

Anderer answered 11/6, 2014 at 8:11 Comment(2)
Sorry if it sounds silly but the metainformation mentionned in the RFC are <META> tags in the header right ? So I'm free to update these but not information the <body> ?Rearward
If you are writing the server part, then yes. If you are writing the client part - then you won't change these because they will be set from the server as part of the response.Anderer
M
7

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.

Marriageable answered 11/6, 2014 at 8:10 Comment(0)
A
5

Does the "document view" refers to the DOM ?

Not exactly, it means the rendered document. DOM is not a representation of the rendered document. It is is abstract model of the documents tree. Each UA may render it differently.

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?

This depends on the API and how 'conformant' it is. So, if you submitted a delete request using the 'DELETE' verb (so, its a RESTful API), then the response should be 200 OK; but it can also send a 204 if the API is especially pedantic.

If you are just sending a GET request to '/some/view?id=1&action=delete', it will most likely return 200 OK

In either case, you are free to change the DOM (and thus, trigger a re-rendering of the document) because of the second part:

although any new or updated metainformation SHOULD be applied to the document currently in the user agent's active view.

So, using <strike> to indicate that the record has been deleted would conform to this requirement.

Anderer answered 11/6, 2014 at 8:11 Comment(2)
Sorry if it sounds silly but the metainformation mentionned in the RFC are <META> tags in the header right ? So I'm free to update these but not information the <body> ?Rearward
If you are writing the server part, then yes. If you are writing the client part - then you won't change these because they will be set from the server as part of the response.Anderer

© 2022 - 2024 — McMap. All rights reserved.