How to access the HTTP request Headers in a Flask app

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
4 min

banner

# Table of Contents

  1. How to access the HTTP request Headers in a Flask app
  2. Specifying a default header value for headers that are not set
  3. Accessing header values with bracket notation
  4. Converting the request headers object to a native Python dictionary
  5. Getting the query parameters in Flask

# How to access the HTTP request Headers in a Flask app

To access the HTTP request headers in a Flask application:

  1. Import the request object from the flask module.
  2. Use the request.headers.get() method to access the HTTP request headers.
app.py
from flask import Flask, request app = Flask(__name__) @app.route("/") def home_route(): print(request.headers) print('------') print(request.headers.get('Authorization')) print(request.headers.get('Content-Type')) print(request.headers.get('Accept')) return "<p>Home: bobbyhadz.com</p>" if __name__ == '__main__': app.run(debug=True, host='localhost', port=8000)

Now, I'll start my Flask application with python app.py.

shell
python app.py # Or with python3 python3 app.py

Suppose, I send an HTTP GET request to http://localhost:8000 with the following request headers:

shell
Authorization: ABC123 Accept: application/json Content-Type: application/json

Here is the output in my terminal.

get http request headers in flask application

The first step is to import the request object from flask.

ap.py
from flask import Flask, request

You can then access the request headers by passing a specific header to the request.headers.get() method.

app.py
print(request.headers.get('Authorization')) print(request.headers.get('Content-Type')) print(request.headers.get('Accept'))

The request.headers property returns the headers that were received with the request.

app.py
print(request.headers)

The headers object has a get() method that is similar to the dict.get() method.

The request.headers.get() method takes a header key as a parameter and returns the corresponding request header value.

# Specifying a default header value for headers that are not set

You can also pass a second argument to the request.headers.get() method - a default value that is returned if the specified request header is not set.

app.py
from flask import Flask, request app = Flask(__name__) @app.route("/") def home_route(): print(type(request.headers)) print('------') # 👇️ With a default value print(request.headers.get('Authorization', 'default value')) print(request.headers.get('Content-Type', 'default value')) print(request.headers.get('Accept', 'default value')) return "<p>Home: bobbyhadz.com</p>" if __name__ == '__main__': app.run(debug=True, host='localhost', port=8000)
If you don't pass a default value to the request.headers.get() method and the specified header doesn't exist, None is returned.

If I now issue the HTTP GET request to http://localhost:8000 without setting any headers, the default values are printed.

default header values printed

# Accessing header values with bracket notation

The request.headers object is similar to a native Python dictionary, so you can also use bracket notation to get the values of headers.

app.py
from flask import Flask, request print(request.headers['Authorization']) print(request.headers['Content-Type']) print(request.headers['Accept'])

However, if you use bracket notation, you will get a KeyError exception if the specified request header is not set.

You can use an if statement if you need to avoid getting the error.

app.py
from flask import Flask, request app = Flask(__name__) @app.route("/") def home_route(): print(request.headers) print('------') if 'Authorization' in request.headers: print(request.headers['Authorization']) else: print('Authorization header is NOT set') return "<p>Home: bobbyhadz.com</p>" if __name__ == '__main__': app.run(debug=True, host='localhost', port=8000)

The if statement uses the in operator to check if the Authorization key is set in the request.headers object.

If the key is set, its value gets printed, otherwise, the else block runs.

check if request header set

# Converting the request headers object to a native Python dictionary

You can also use the dict class to convert the request headers object to a native Python dictionary.

app.py
from flask import Flask, request app = Flask(__name__) @app.route("/") def home_route(): # ✅ Convert the headers object to a native Python dictionary print(dict(request.headers)) return "<p>Home: bobbyhadz.com</p>" if __name__ == '__main__': app.run(debug=True, host='localhost', port=8000)

convert request headers to native python dictionary

Passing the request.headers object to the dict() class converts the object to a native Python dictionary.

# Getting the query parameters in Flask

You can also use the request object to get the query parameters in a Flask application.

app.py
from flask import Flask, request app = Flask(__name__) @app.route("/") def home_route(): page = request.args.get('page') print('page: ', page) limit = request.args.get('limit') print('limit: ', limit) return "<p>Home: bobbyhadz.com</p>" if __name__ == '__main__': app.run(debug=True, host='localhost', port=8000)
  1. I will start my development server with python app.py.
shell
python app.py python3 app.py
  1. Now, I'll issue an HTTP GET request to the following URL: http://localhost:8000?page=10&limit=5.

Notice that the page and limit query parameters are set.

Here is the output in my terminal.

get query parameters in flask

The request.args.get() method takes the name of the query parameter and returns the corresponding value.

app.py
page = request.args.get('page') print('page: ', page) limit = request.args.get('limit') print('limit: ', limit)

If the query parameter is not set, then None is returned.

The method also takes a second argument - a default value that gets returned when the query parameter is not set.

app.py
page = request.args.get('page', 'default value') print('page: ', page) limit = request.args.get('limit', 'default value') print('limit: ', limit)

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.