When interacting with websites and APIs, the servers return status codes to inform if the request has been processed successfully or not.
One of those codes is the 431 or Request Header Fields Too Large error, and in this article we’ll explain what it is, when it shows up, and possible ways to avoid it.
Also, if you are working with an API, we’ll give you a handy resource to test the 431 HTTP status code from your scripts.
What is the HTTP Error 431?
This error belongs to the family of 4xx HTTP status codes, indicating that the problem’s source is on the client side.
More precisely, the 431 error is returned when the server can’t process the request because, you guessed it, the headers are too large. Let’s see why this happens.
What causes the HTTP Error 431?
If you are the 431 error as a response to an API request, it’s possible you are sending too many headers in the request.
This response might be caused by a single header exceeding the maximum allowed size, in which case the server should return which one is causing the issue, or because the entire set of headers surpasses the limit.
The more common scenarios you might encounter this error are:
- Cookies size: you are sending too many cookies in the request or they are too large.
- URL length or
Referer
header: the URL you are trying to access or theReferer
header is too large.
How can I fix the HTTP Error 431?
There are several ways to address the 431 error. Some can be managed from the client side, while others require changes on the server.
If you can only access the API from the client side, try the following:
- Reduce the size of the cookies you are sending.
- Remove the
Referer
header or shorten the URL by modifying the query parameters. - Include only the necessary headers in the request.
- If you are using a proxy or firewall, check that they are not adding additional headers that could be causing the issue.
If you have access to the server, you can modify the configuration to prevent the error by:
- Increasing the allowed size of headers.
- Enabling header compression.
- Enabling HTTP version 2 (HTTP/2), which allows the use of compressed headers, reducing the amount of data sent and increasing performance.
How can I avoid the HTTP Error 431?
To avoid the 431 error, it’s recommended to keep request headers as small as possible and adjust server configuration when feasible.
Optimizing the information you send will not only help prevent this error but also improve the performance of your API calls.
How can I test the HTTP Error 431?
If you’re working with an API and need to reproduce a specific HTTP status code during your testing, you can do so through httpstatus.is. It’s a very simple service designed to help you check how your code handles different HTTP responses.
To test the 431: Request Header Fields Too Large code, call the httpstatus.is/431 endpoint.
The server will return a JSON response similar to this:
HTTP/1.1 431 Request Header Fields Too Large
Content-Length: 76
Content-Type: application/json; charset=utf-8
x-powered-by: www.helloapi.co
{
"code": 431,
"description": "Request Header Fields Too Large",
"official": true
}
See also
- Official specification: 431 Request Header Fields Too Large.
- httpstatus.is on GitHub.