How to get http headers in flask?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In Flask, incoming HTTP headers are available through the request object. Accessing them is simple, but using them correctly matters because headers are central to authentication, tracing, content negotiation, and proxy-aware request handling.
The practical rule is to read headers through request.headers, validate anything important, and avoid trusting client-supplied values blindly. That gives you code that works both in local testing and behind real reverse proxies.
Read Headers With request.headers
Flask exposes request headers as a case-insensitive mapping:
Using .get() is preferable to direct indexing because missing headers are common and should not usually raise an exception.
If you need to inspect everything during debugging, convert the header mapping to a normal dictionary:
That is useful when debugging client libraries, proxies, or webhook calls.
Handle Common Header Use Cases
Header access becomes more useful when tied to real behaviors.
Authorization:
Language preference:
Request tracing:
These examples show the normal pattern: read the header, validate it if necessary, and convert it into application logic.
Validate Important Headers
Headers come from the client, so treat them as untrusted input unless they are inserted by a trusted gateway. A small validation helper keeps the rules clear:
This is especially important for custom headers that affect authorization or routing decisions.
Think About Reverse Proxies
In production, Flask often sits behind Nginx, Apache, a cloud load balancer, or an API gateway. That means some headers may be rewritten, added, or forwarded by infrastructure.
For example, values such as X-Forwarded-For or X-Forwarded-Proto should only be trusted when the proxy setup is known and controlled. If you need Flask to understand those forwarded values, configure proxy middleware explicitly:
Do this only when your deployment topology is well defined. Otherwise you risk trusting spoofed headers from untrusted clients.
Set Response Headers Too
Many applications need not only to read request headers but also to write response headers:
This is how you add caching directives, trace metadata, or security policies to the response.
Common Pitfalls
The biggest mistake is assuming a header is always present and indexing it directly. Missing headers are normal, so .get() with validation is usually the better pattern.
Another common issue is trusting forwarded or custom identity headers from arbitrary clients. If a proxy is responsible for injecting those headers, the trust boundary needs to be explicit.
People also log sensitive headers such as authorization tokens during debugging. That can create credential leaks in logs very quickly.
Finally, do not confuse request headers with response headers. Reading from request.headers and writing to response.headers are different parts of the API.
Summary
- Read incoming headers in Flask through
request.headers. - Use
.get()so missing headers are handled safely. - Validate headers that affect authentication, routing, or business logic.
- Treat proxy-forwarded headers carefully and only trust them in controlled deployments.
- Set response headers explicitly when you need caching, tracing, or security metadata.
Related reading
- How to get HTTP response code for a URL in Java?
- How to get IP address of the device from code?
- How to get json response using system.net.webrequest in c?
- How to get Kubernetes cluster name from K8s API
- How to get indices of a sorted array in Python
- How to get JSON from webpage into Python script
- How to get mac host IP address from a docker container?
- How to get my IP address programmatically on iOS/macOS?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.