Spring Boot request header return null value
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
When @RequestHeader returns null in Spring Boot, the header either was not sent by the client, was misspelled, or was stripped by a proxy or load balancer before reaching your application. Spring is case-insensitive for header names, so the issue is almost always about the header being absent rather than a casing mismatch. The fix depends on which scenario applies: make the header optional with required = false, check your client request, or inspect your reverse proxy configuration.
How @RequestHeader Works
If the X-Custom-Token header is missing from the request, Spring throws MissingRequestHeaderException (400 Bad Request) by default — not null. You only get null when you explicitly make the header optional.
Cause 1: Header Not Sent by Client
The most common cause. Verify the header is actually being sent:
In JavaScript fetch calls, headers are often missing due to typos:
Cause 2: Header Made Optional Without Default
Cause 3: Proxy or Load Balancer Stripping Headers
Reverse proxies (Nginx, Apache, AWS ALB) can strip or rename custom headers:
AWS Application Load Balancer strips headers with underscores by default. Use hyphens instead: X-Custom-Token instead of X_Custom_Token.
Cause 4: CORS Preflight Dropping Headers
Browser CORS preflight requests (OPTIONS) do not include custom headers. If your controller handles OPTIONS requests, the header will be absent:
The allowedHeaders configuration must include your custom header name, or the browser will not send it in the actual request.
Cause 5: Using HttpServletRequest Directly
HttpServletRequest.getHeader() always returns null for missing headers without throwing an exception, unlike @RequestHeader with required = true.
Debugging Headers
Call this endpoint to see exactly which headers arrive at your application.
Using Optional for Nullable Headers
Common Pitfalls
- Assuming
nullmeans the annotation failed:@RequestHeaderwithrequired = true(default) throws a 400 error, notnull. If you seenull, you made the header optional somewhere. - Underscore in header names: Nginx drops headers with underscores (
X_Custom_Token) by default. Use hyphens (X-Custom-Token) or enableunderscores_in_headers on. - Testing with browser DevTools: The browser may not send custom headers on GET requests unless you use
fetch()orXMLHttpRequest. Direct URL bar navigation sends no custom headers. - Case sensitivity confusion: HTTP header names are case-insensitive per the spec, and Spring handles this correctly.
x-custom-tokenandX-Custom-Tokenare the same header. - Spring Security intercepting requests: If Spring Security is configured, it may reject requests before they reach your controller. Check your security filter chain if headers seem to disappear.
Summary
@RequestHeaderthrows 400 by default for missing headers — userequired = falseto getnullinstead- Always verify the client is actually sending the header (use curl or browser DevTools)
- Reverse proxies often strip custom headers with underscores — use hyphens
- Add
allowedHeadersin CORS configuration for custom headers from browsers - Use
HttpServletRequest.getHeader()orOptional<String>for graceful null handling - Create a debug endpoint to inspect all incoming headers when troubleshooting
Related reading
- Spring Boot requests to re-run your application with 'debug' enabled - how do I?
- Spring boot ResponseBody doesn't serialize entity id
- Spring Boot REST API - request timeout?
- Spring Boot Rest Controller how to return different HTTP status codes?
- Spring Boot REST service exception handling
- Spring Boot REST service exception handling
- Spring boot REST validation error response
- Spring Boot Security CORS

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.