Last updated: Apr 11, 2024
Reading time·4 min
To access the HTTP request headers in a Flask application:
request
object from the flask
module.request.headers.get()
method to access the HTTP request headers.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
.
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:
Authorization: ABC123 Accept: application/json Content-Type: application/json
Here is the output in my terminal.
The first step is to import the request
object from flask
.
from flask import Flask, request
You can then access the request headers by passing a specific header to the
request.headers.get()
method.
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.
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.
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.
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)
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.
The request.headers
object is similar to a native Python dictionary, so you
can also use bracket notation to get the values of headers.
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.
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.
You can also use the dict
class to convert the request headers object to a
native Python dictionary.
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)
Passing the request.headers
object to the dict()
class converts the object
to a native Python dictionary.
You can also use the request
object to get the query parameters in a Flask
application.
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)
python app.py
.python app.py python3 app.py
http://localhost:8000?page=10&limit=5
.Notice that the page
and limit
query parameters are set.
Here is the output in my terminal.
The request.args.get()
method takes the name of the query parameter and
returns the corresponding value.
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.
page = request.args.get('page', 'default value') print('page: ', page) limit = request.args.get('limit', 'default value') print('limit: ', limit)
You can learn more about the related topics by checking out the following tutorials: