To parse the Authorization header in Python, you can use the Authorization
module from the http
library. The Authorization
module is a subclass of the HTTPHeader
module, which allows us to parse the header fields from an HTTP request or response message.
Here's a sample code that shows how to parse the Authorization header using Authorization
module in Python:
main.py547 chars18 lines
In this example, we split the Authorization header string into two parts using the split
method. We then check if the first part is "Bearer", and if there are exactly two parts. If these checks pass, we extract the access token from the second part.
We then use the access token to authenticate the request by adding it to the Authorization
header of the outgoing request. Finally, we print the response from the API endpoint.
gistlibby LogSnag