'Should a RESTful 'PUT' operation return something....
I was wondering what people's opinions are of a RESTful PUT
operation that returns nothing (null) in the response body.
Solution 1:[1]
As opposed to most of the answers here, I actually think that PUT should return the updated resource (in addition to the HTTP code of course).
The reason why you would want to return the resource as a response for PUT operation is because when you send a resource representation to the server, the server can also apply some processing to this resource, so the client would like to know how does this resource look like after the request completed successfully. (otherwise it will have to issue another GET request).
Solution 2:[2]
I think it is possible for the server to return content in response to a PUT. If you are using a response envelop format that allows for sideloaded data (such as the format consumed by ember-data), then you can also include other objects that may have been modified via database triggers, etc. (Sideloaded data is explicitly to reduce # of requests, and this seems like a fine place to optimize.)
If I just accept the PUT and have nothing to report back, I use status code 204 with no body. If I have something to report, I use status code 200, and include a body.
Solution 3:[3]
If the backend of the REST API is a SQL relational database, then
- you should have RowVersion in every record that can be updated (to avoid the lost update problem)
- you should always return a new copy of the record after PUT (to get the new RowVersion).
If you don't care about lost updates, or if you want to force your clients to do a GET immediately after a PUT, then don't return anything from PUT.
Solution 4:[4]
The HTTP/1.1 spec (section 9.6) discusses the appropriate response/error codes. However it doesn't address the response content.
What would you expect ? A simple HTTP response code (200 etc.) seems straightforward and unambiguous to me.
Solution 5:[5]
Http method "PUT" may have different Http status as per execution status on passed request-URI. The Below table may help to understand -
Solution 6:[6]
Http response code of 201 for "Created" along with a "Location" header to point to where the client can find the newly created resource.
Solution 7:[7]
I used RESTful API in my services, and here is my opinion:
First we must get to a common view? PUT
is used to update an resource not create or get.
I defined resources with: Stateless resource
and Stateful resource
:
Stateless resources For these resources, just return the HttpCode with empty body, it's enough.
Stateful resources For example: the resource's version. For this kind of resources, you must provide the version when you want to change it, so return the full resource or return the version to the client, so the client need't to send a get request after the update action.
But, for a service or system, keep it simple
, clearly
, easy to use and maintain
is the most important thing.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | LiorH |
Solution 2 | shaunc |
Solution 3 | John Henckel |
Solution 4 | Brian Agnew |
Solution 5 | |
Solution 6 | dc360 |
Solution 7 | Bruce |