0
0
Rest APIprogramming~5 mins

204 No Content in Rest API

Choose your learning style9 modes available
Introduction

The 204 No Content status tells the client that the request was successful but there is no data to send back. It helps keep communication clear and efficient.

When a client sends data to update something and you want to confirm success without sending extra data back.
When deleting a resource and you want to say 'done' without returning any content.
When a client sends a request that changes state but does not need any new information returned.
When you want to save bandwidth by not sending unnecessary data after a successful request.
Syntax
Rest API
HTTP/1.1 204 No Content

The status line includes the number 204 and the phrase 'No Content'.

No message body should be sent after this status.

Examples
Basic example of a 204 response with no body.
Rest API
HTTP/1.1 204 No Content

Even if headers like Content-Type are sent, the body must be empty.
Rest API
HTTP/1.1 204 No Content
Content-Type: application/json

Sample Program

This Python Flask program creates a simple web server with a DELETE endpoint. When you call '/delete-item', it returns a 204 No Content response to say the deletion was successful without sending any data back.

Rest API
from flask import Flask, Response

app = Flask(__name__)

@app.route('/delete-item', methods=['DELETE'])
def delete_item():
    # Imagine we delete an item here
    return Response(status=204)

if __name__ == '__main__':
    app.run(debug=True)
OutputSuccess
Important Notes

Clients receiving 204 should not expect any content in the response body.

204 responses must not include a message body, so sending one can cause errors.

Use 204 to improve efficiency when no data needs to be returned after a successful request.

Summary

204 No Content means the request worked but there is no data to send back.

It is useful for actions like delete or update where no response body is needed.

Always send an empty body with 204 to follow the rules and avoid confusion.