I was wondering what people's opinions are of a RESTful PUT
operation that returns nothing (null) in the response body.
The API should always return sensible HTTP status codes. API errors typically break down into 2 types: 400 series status codes for client issues & 500 series status codes for server issues. At a minimum, the API should standardize that all 400 series errors come with consumable JSON error representation.
Responses. If the target resource does not have a current representation and the PUT request successfully creates one, then the origin server must inform the user agent by sending a 201 ( Created ) response.
Another important difference between the methods is that PUT is an idempotent method while POST is not. For instance, calling the PUT method multiple times will either create or update the same resource. On the contrary, multiple POST requests will lead to the creation of the same resource multiple times.
Does the RESTlet framework allow returning data in a POST? Yes, even though it returns void, in a class which extends Resource, you have full access to the Response object object via the getResponse() method.
The HTTP specification (RFC 2616) has a number of recommendations that are applicable. Here is my interpretation:
200 OK
for a successful PUT of an update to an existing resource. No response body needed. (Per Section 9.6, 204 No Content
is even more appropriate.)201 Created
for a successful PUT of a new resource, with the most specific URI for the new resource returned in the Location header field and any other relevant URIs and metadata of the resource echoed in the response body. (RFC 2616 Section 10.2.2)409 Conflict
for a PUT that is unsuccessful due to a 3rd-party modification, with a list of differences between the attempted update and the current resource in the response body. (RFC 2616 Section 10.4.10)400 Bad Request
for an unsuccessful PUT, with natural-language text (such as English) in the response body that explains why the PUT failed. (RFC 2616 Section 10.4)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With