Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it valid to return a body along with an Http 204 status?

We're currently working to the OData standard:

http://www.odata.org/developers/protocols/operations

According to this standard, if you perform an PUT operation against your data, you should return a 204 status code. The standard says that:

When processing a PUT request servers return status 204 (No Content) to indicate success, no response body is needed.

Now, according to what one reads elsewhere, some places claim that if you're returning a 204 you should absolutely NOT return a response body - it's not a question of whether one is needed or not.

The problem is we kind of need to return a body. In this case we're performing an update query on a batch of items and partial success (i.e. some are updated, some are not) is possible. We'd like to inform the client of what the failures were and the response body is the only way of doing it that I can think of.

So what's the "correct" way of doing this. Should I flout the W3C and return a body? Or should I flout OData and return a different response code? Or is there another possibility?

like image 693
Bob Tway Avatar asked Feb 07 '12 11:02

Bob Tway


2 Answers

The 204 response to PUT is not a mandatory requirement for OData servers (note that the odata.org is not a normative reference, the MS-OData document is). You can return 200 with body, in which case the body you return should be a serialization of the updated entity (after the updates were applied by the server). In fact in OData V3 we now have a support for Prefer header which does exactly this. The client can include a Prefer header with the PUT request and ask the server to respond with 200 and the updated entityt.

like image 146
Vitek Karas MSFT Avatar answered Sep 22 '22 13:09

Vitek Karas MSFT


I think you must not return content and should consider returning a header to indicate the partially complete command.

The spec states

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

You may also wish to take the batch approach, http://www.odata.org/developers/protocols/batch

like image 26
Mike Miller Avatar answered Sep 22 '22 13:09

Mike Miller