HTTP 204 No Content

Overview

The HTTP 204 No Content status code indicates that the server successfully processed the request, but is not returning any content.

Purpose

The HTTP 204 response is typically used to indicate a successful request where no further action is needed from the client, often used in AJAX requests or after DELETE operations in APIs.

Usage

Client Behavior:

  1. Send Request: The client sends an HTTP request that does not require a response body.
  2. Receive Response: The client receives the HTTP 204 status code, indicating successful processing with no content to return.

Server Behavior:

  1. Process Request: The server processes the received request.
  2. Send Response: The server sends a 204 No Content response, indicating successful processing without any additional content.

Scenarios

  • AJAX Requests: Used in AJAX operations where the client does not require a new page load or additional content.
  • API DELETE Operations: Commonly returned after successful DELETE requests in RESTful APIs.

Sequence Diagram

A sequence diagram illustrating the request and response flow for HTTP 204:

sequenceDiagram
    participant Client
    participant Server as Web Server

    Note over Client: Client sends a request
    Client->>Server: DELETE /item/123 HTTP/1.1
    Note over Server: Server processes the request
    Server->>Client: HTTP/1.1 204 No Content

Curl Request and Response Example

Sending a DELETE request using Curl and expecting a 204 No Content response:

curl -i -X DELETE http://example.com/item/123
# Expected response: HTTP/1.1 204 No Content

PHP cURL Request and Response Example

PHP code using cURL to send a DELETE request and handle a 204 No Content response:

<?php
$ch = curl_init('http://example.com/item/123');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == 204) {
    echo "Item deleted successfully, no content returned.";
}
curl_close($ch);
?>

Python Request and Response Example

Python script using the requests library to send a DELETE request and check for a 204 No Content response:

import requests
response = requests.delete('http://example.com/item/123')
if response.status_code == 204:
    print("Item deleted successfully, no content returned")

Apache Configuration for HTTP 204 No Content

Configuring Apache to serve resources that might result in a 204 No Content response:

<VirtualHost *:80>
    ServerName example.com
    # Configuration directives...
</VirtualHost>

NGINX Configuration for HTTP 204 No Content

Setting up NGINX to handle DELETE requests that may return a 204 No Content response:

server {
    listen 80;
    server_name example.com;

    location /item {
        # Proxy pass or other directives for DELETE operations
        proxy_pass http://backend_server;
        # Additional configurations...
    }
}

HTTP 203 Non-Authoritative Information HTTP 205 Reset Content


 

Free Weekly

Newsletter

Join my weekly newsletter for the latest in tech! You'll get neat coding tricks, trend updates, career advice, SaaS reviews, crypto, bitcoin, and financial tips. All straight to your inbox, designed to keep you ahead.