Let's say I have an API where you can get users:
GET /RESTAPI/user/
And you can delete users by:
DELETE /RESTAPI/user/123
What is the RESTful convention on what the DELETE's response body should contain? I expected it should be the new list of all users which now doesn't contain the user with id 123 anymore.
Googling around didn't get me any satisfying answers. I only found opinions on how to do that, but isn't there a strict definition of RESTful Services?
This is NOT a duplicate of What should a RESTful API POST/DELETE return in the body? and What REST PUT/POST/DELETE calls should return by a convention? since this questions asks for a strict definition regarding DELETE. Those questions were only answered by loose opinions only.
DELETE API Response Codes. A successful response of DELETE requests SHOULD be an HTTP response code 200 (OK) if the response includes an entity describing the status. The status should be 202 (Accepted) if the action has been queued.
The short answer is: You should include a response body with an entity describing the deleted item/resource if you return 200. 202 is something like an asynchronous request/response return status.
A 204 ( No Content ) status code if the action has been enacted and no further information is to be supplied. A 200 ( OK ) status code if the action has been enacted and the response message includes a representation describing the status.
According to Mozilla a DELETE request "may" have a body, compared to a PUT, which should have a body. By this it seems optional whether you want to provide a body for a DELETE request.
The reason you get no hard answers is because there is no hard RESTful standard. So I can only suggest that you create a hard standard and stick to it within your own APIs
I used this as a guide for RESTful services http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api
It says respond with a 204 status and an empty body
I stick to those standards and document them well for anyone who wants to use my APIs
What is the RESTful convention on what the
DELETE
's response body should contain?
REST is an architectural style defined by Fielding in the chapter 5 of his dissertation and it describes a set of contraints for applications built with this architecture. REST is designed to be protocol indenpendent but the chapter 6 of the same dissertation describes how REST is applied over HTTP.
Once your REST application is designed on the top of the HTTP protocol, you should be aware of the HTTP semantics. And the semantis of the HTTP/1.1 protocol are currently described in the RFC 7231.
The response payload of a DELETE
request that has succeeded may:
And the following response status codes are suitable for a DELETE
request that has succeeded:
202
: The request has been accepted for processing, but the processing has not been completed.204
: The server has successfully fulfilled the request and that there is no additional content to send in the response payload body.200
: The request has succeeded and the request payload includes a representation of the status of the action.See the following quote from the RFC 7231:
If a
DELETE
method is successfully applied, the origin server SHOULD send a202
(Accepted) status code if the action will likely succeed but has not yet been enacted, a204
(No Content) status code if the action has been enacted and no further information is to be supplied, or a200
(OK) status code if the action has been enacted and the response message includes a representation describing the status.
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