The 406 Not Acceptable
HTTP status code is a server response indicating that the server cannot produce a response that matches the list of acceptable values defined in the request’s headers.
Overview
When a client sends an HTTP request to a server, it can include an Accept
header that specifies the media types that the client can understand. The server should then respond with a representation of the requested resource in one of the specified media types. If the server cannot generate a response that matches any of the media types specified in the Accept
header, it should return a 406 Not Acceptable
status code.
The primary purpose of the 406 Not Acceptable
status code is to inform the client that the server cannot fulfill the request with the desired media type. This situation may arise when the server does not support the requested media type or when the client has made an error in specifying the acceptable media types.
Example
Consider the following HTTP request:
GET /example-resource HTTP/1.1
Host: example.com
Accept: application/xml
In this request, the client is asking for the /example-resource
endpoint and specifies that it only accepts responses in the application/xml
media type.
Now, let’s assume that the server can only generate a response in the application/json
media type. The server will then respond with a 406 Not Acceptable
status code, as shown below:
HTTP/1.1 406 Not Acceptable
Content-Type: text/plain
Content-Length: 89
The requested resource is only capable of generating content in application/json media type.
The response informs the client that the server cannot generate a response in the application/xml
media type, as requested.
Causes
There are two main reasons why a server might return a 406 Not Acceptable
status code:
- Server limitation: The server does not support the requested media type(s). In this case, the server is unable to fulfill the request, and the client must either modify its request or seek the resource elsewhere.
- Client error: The client has made an error in specifying the acceptable media types in the
Accept
header. This might be due to a typo, an overly restrictive list, or a misunderstanding of the media types supported by the server. In this case, the client should correct theAccept
header and resend the request.
Handling 406 Not Acceptable
To handle a 406 Not Acceptable
status code, follow these steps:
- Check the
Accept
header: Ensure that theAccept
header in the request is correct and includes all the media types that the client can understand. If the header is incorrect, modify it and resend the request. - Verify server capabilities: If the
Accept
header is correct, verify the server’s supported media types. This information might be available in the server’s documentation or API specifications. If the server does not support the requested media type, consider requesting the resource in a different, supported media type or looking for an alternative server that can provide the desired media type. - Contact the server administrator: If the issue persists, contact the server administrator or the support team for assistance. They might be able to provide further guidance or help resolve the issue.
Summary
The 406 Not Acceptable
HTTP status code indicates that a server cannot generate a response that matches the list of acceptable media types specified in the client’s request. This can be due to server limitations or errors in the client’s Accept
header. To handle this status code, clients should verify their Accept
header, check the server’s supported media types, and, if necessary, contact the server administrator for assistance.