HTTP 406 Not Acceptable

Overview

The HTTP 406 Not Acceptable status code is used when the server cannot produce a response matching the list of acceptable values defined in the request’s proactive content negotiation headers, such as Accept, Accept-Charset, Accept-Encoding, and Accept-Language.

Purpose

The HTTP 406 response indicates that the server can generate a response for the requested resource, but that response is not acceptable according to the Accept headers sent in the request.

Usage

Client Behavior:

  1. Send Request with Accept Headers: The client sends an HTTP request including headers like Accept to specify the response formats it is willing to receive.
  2. Receive Response: The client receives the HTTP 406 status code if the server cannot provide a response in any of the acceptable formats.

Server Behavior:

  1. Evaluate Accept Headers: The server evaluates the request’s Accept headers against its capabilities to produce a response.
  2. Send Response: If no acceptable formats are available, the server sends a 406 Not Acceptable response.

Scenarios

  • Content Negotiation: When the client specifies a particular format for the response which the server cannot fulfill.
  • Limited Resource Formats: Resources that are only available in formats not acceptable to the client.

Sequence Diagram

Illustrating the process for an HTTP 406 response:

sequenceDiagram
    participant Client
    participant Server as Web Server

    Note over Client: Client requests with specific accept headers
    Client->>Server: GET /resource HTTP/1.1
    Client->>Server: Accept: application/xml
    Note over Server: Server cannot provide XML format
    Server->>Client: HTTP/1.1 406 Not Acceptable

Curl Request and Response Example

Requesting a resource with specific Accept headers using Curl:

curl -i -H "Accept: application/xml" http://example.com/resource
# Expected response: HTTP/1.1 406 Not Acceptable

PHP cURL Request and Response Example

PHP script using cURL to handle a 406 Not Acceptable response:

<?php
$ch = curl_init('http://example.com/resource');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/xml'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == 406) {
    echo "Requested format not acceptable.";
}
curl_close($ch);
?>

Python Request and Response Example

Python script to send a GET request with specific Accept headers and handle a 406 Not Acceptable response:

import requests
headers = {'Accept': 'application/xml'}
response = requests.get('http://example.com/resource', headers=headers)
if response.status_code == 406:
    print("Requested format not acceptable")

Apache Configuration for HTTP 406 Not Acceptable

Configuring Apache to handle content negotiation and 406 responses:

<VirtualHost *:80>
    ServerName example.com
    # Configuration for content negotiation and handling 406 Not Acceptable
    # ...
</VirtualHost>

NGINX Configuration for HTTP 406 Not Acceptable

Setting up NGINX to manage content negotiation and 406 responses:

server {
    listen 80;
    server_name example.com;
    location /resource {
        # Directives for handling different Accept headers
        # ...
    }
}

HTTP 405 Method Not Allowed HTTP 407 Proxy Authentication Required


 

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.