The 204 No Content
status code is part of the Hypertext Transfer Protocol (HTTP) response status codes. This status code indicates that the server has successfully processed the request and there is no additional content to send in the response payload body. This is typically used when a request has been successfully fulfilled and there is no need to return any data.
When to Use 204 No Content
The 204 No Content
status code is commonly used in the following scenarios:
- Updating Resources: When a client sends a request to update a resource on the server, and the server has successfully updated the resource without needing to return any data in the response body.
- Deleting Resources: When a client sends a request to delete a resource on the server, and the server has successfully deleted the resource without needing to return any data in the response body.
- Form Submissions: When a client submits a form and the server has successfully processed the form data without needing to return any data in the response body.
Example
Request
In this example, a client sends a PATCH
request to update a resource on the server.
PATCH /api/v1/users/123 HTTP/1.1
Host: example.com
Content-Type: application/json
{
"email": "new-email@example.com"
}
Response
The server successfully updates the user’s email and returns a 204 No Content
status code.
HTTP/1.1 204 No Content
Date: Mon, 07 Jun 2023 12:34:56 GMT
Handling 204 No Content in Clients
When receiving a 204 No Content
response, clients should not expect any data in the response body. Instead, clients should interpret the response as a successful completion of the requested action.
For example, if a client sends a request to update a resource and receives a 204 No Content
response, the client should assume the resource has been successfully updated, and there is no need to fetch the updated resource again.
Summary
In summary, the 204 No Content
status code is used to indicate that a request has been successfully processed, and there is no additional content to send in the response payload body. This status code is commonly used when updating or deleting resources, as well as processing form submissions. When handling a 204 No Content
response, clients should not expect any data in the response body and should interpret the response as a successful completion of the requested action.