About 204 No Content
Overview of 204 No Content
Successful Response Without Contentin an HTTP Status Code | ||
204 No Content Overview The 204 No Content status code is used when a request is successfully processed, but there is no content to return in the response body. This code is appropriate when no additional data needs to be sent back as part of the response to the client’s request. |
||
Meaning The client’s request was successfully processed, but no content is returned in the response body. |
When is 204 No Content Returned?
- When a request is successful but there is no data to return
- When the server has accepted a request and the state of the resource remains unchanged
- When only triggering an asynchronous process, and no immediate response data is needed
Examples of 204 No Content
Resource Update Request
PUT /api/resource/123 HTTP/1.1 Host: example.com Content-Type: application/json { "name": "Updated Resource", "status": "active" }
Response example:
HTTP/1.1 204 No Content
Explanation: In this example, a client sends a PUT request to update a resource, and the request is successful. The server returns a 204 status code without a response body to indicate success without additional data.
Resource Deletion Request
DELETE /api/resource/123 HTTP/1.1 Host: example.com
Response example:
HTTP/1.1 204 No Content
Explanation: A DELETE request successfully removes a resource, and a 204 status code is returned to indicate success. No response body is needed, as the operation itself signifies the action was completed.
Points to Note
Considerations when using the 204 No Content status code:
- Do not include a response body
A 204 response must not include a body. Including one can confuse clients. - Set headers appropriately
Avoid or correctly set content-related headers such asContent-Type
andContent-Length
.
Comparison with Related HTTP Status Codes
Here is an explanation of status codes related to 204 No Content:
- 200 OK: Used when a request is successful, and data is included in the response body.
- 202 Accepted: Used when a request is accepted but processing is not yet complete.
Understanding these differences helps in appropriately using the 204 status code.