Resolving "Request header is too large" Error in Tomcat: HTTP Method Selection and Configuration Optimization

Dec 04, 2025 · Programming · 10 views · 7.8

Keywords: Tomcat | HTTP Request Header Too Large | GET vs POST Methods

Abstract: This paper delves into the "Request header is too large" error encountered in Tomcat servers, typically caused by oversized HTTP request headers. It first analyzes the root causes, noting that while the HTTP protocol imposes no hard limit on header size, web servers like Tomcat set default restrictions. The paper then focuses on two main solutions: optimizing HTTP method selection by recommending POST over GET for large data transfers, and adjusting server configurations, including modifying Tomcat's maxHttpHeaderSize parameter or Spring Boot's server.max-http-header-size property. Through code examples and configuration instructions, it provides practical steps to effectively avoid this error, enhancing the stability and performance of web applications.

Background and Error Analysis

In Java web applications based on Tomcat, developers often encounter the "Request header is too large" error, with a stack trace as shown below:

INFO: Error parsing HTTP request header
Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.
java.lang.IllegalArgumentException: Request header is too large
    at org.apache.coyote.http11.InternalInputBuffer.fill(InternalInputBuffer.java:512)
    at org.apache.coyote.http11.InternalInputBuffer.fill(InternalInputBuffer.java:501)
    at org.apache.coyote.http11.InternalInputBuffer.parseRequestLine(InternalInputBuffer.java:171)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:996)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:623)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:722)

This error indicates that the HTTP request header exceeds Tomcat's default parsing limit. Although the HTTP protocol specification (e.g., RFC 7230) does not define a maximum size for request headers, in practice, web servers impose safety limits to prevent buffer overflow attacks or resource exhaustion. Tomcat's default limit is 4096 bytes (4KB), and when the request header (including the request line and all header fields) surpasses this value, an exception is thrown.

Core Solution: HTTP Method Optimization

Based on best practices, the primary solution is to optimize the selection of HTTP methods. In web development, GET and POST methods differ significantly: GET encodes parameters in the URL, while POST transmits data via the request body. Due to browser and server limitations on URL length (typically 2KB to 8KB), using GET for large data transfers can easily trigger the request header overflow error.

For example, consider a Spring web application that needs to transmit data with numerous query parameters. Using GET may result in an excessively long URL:

// Not recommended: Using GET for large parameters
@GetMapping("/api/data")
public ResponseEntity<String> getData(@RequestParam Map<String, String> params) {
    // Processing logic
    return ResponseEntity.ok("Data processed");
}

Switching to POST avoids this issue, as parameters are sent in the request body,不受URL length restrictions:

// Recommended: Using POST for large parameters
@PostMapping("/api/data")
public ResponseEntity<String> postData(@RequestBody Map<String, String> params) {
    // Processing logic
    return ResponseEntity.ok("Data processed");
}

In practical scenarios, if the URL length exceeds 2000 characters, it is strongly advised to switch to POST or split the request. This not only resolves the header size issue but also aligns with RESTful API design principles, where GET is for retrieving resources and POST for creating or processing data.

Server Configuration Adjustment

If optimizing the HTTP method is not feasible (e.g., to maintain GET for compatibility with existing clients), server configuration must be adjusted. Tomcat provides the maxHttpHeaderSize parameter to control the maximum size of request headers. The default is 4096 bytes, which can be increased based on application needs.

In Tomcat's server.xml configuration file, modify the HTTP connector settings:

<Connector port="8080" protocol="HTTP/1.1"
           maxHttpHeaderSize="65536"
           connectionTimeout="20000"
           redirectPort="8443" />

This configuration raises the header limit to 65536 bytes (64KB). Note that excessively increasing this value may raise memory consumption and security risks; it should be set cautiously according to actual data requirements.

For Spring Boot applications, configuration can be done via application.properties or application.yml:

# application.properties
server.max-http-header-size=10000000  # Set to 10MB
# application.yml
server:
  max-http-header-size: 10MB

Spring Boot 2.x and above support this property, which maps directly to Tomcat's maxHttpHeaderSize. In Spring Boot 1.5.8, it is also applicable, but version compatibility should be ensured.

Supplementary Solutions and Best Practices

In addition to the above methods, consider the following supplementary approaches:

Best practices include: prioritizing POST for large data transfers; when GET is necessary, ensuring URL length stays within browser limits (e.g., less than 2000 characters); regularly reviewing server configurations to adjust maxHttpHeaderSize based on application load; and implementing input validation to prevent malicious oversized requests.

Conclusion

The key to resolving the "Request header is too large" error in Tomcat lies in understanding HTTP method differences and server limitations. By optimizing method selection (switching from GET to POST) and adjusting configurations (increasing maxHttpHeaderSize), developers can effectively avoid this error and enhance application stability. In practical development, it is recommended to choose solutions based on specific scenarios and adhere to security best practices to balance functionality and performance.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.