7

I am trying to create an API that receives arguments from postman. The body of the api contains two arguments:

{
    "db":"EUR",
    "env":"test"
}

I parsed these two arguments in the code as below:

parser = reqparse.RequestParser()
parser.add_argument('fab', type=str, required=True, help='Fab name must be provided.')
parser.add_argument('env', type=str, required=False, help='Env is an optional parameter.')

Lately I was asked to add a token validation in the code. The token is passed from Authorization-> Type(Bearer Token) -> Token value: eeb867bd2bcca05

enter image description here

But I don't know how can I read the bearer token from postman into Python code. Could anyone let me know how to read the token value that is being passed from Postman's bearer token into my Python code ? Any help is much appreciated.

Metadata
  • 2,127
  • 9
  • 56
  • 127
  • In a recent project I used a token argument (for password reset via email) and set the URI to the flask function (`url_for`) and passed in the token parameter of the URI as an argument to the function. Then used sub modules to parse (do stuff) with the token. Just a thought ... – S3DEV Aug 21 '20 at 07:43

1 Answers1

16

The Bearer token is sent in the headers of the request as 'Authorization' header, so you can get it in python flask as follows:

headers = flask.request.headers
bearer = headers.get('Authorization')    # Bearer YourTokenHere
token = bearer.split()[1]  # YourTokenHere
AnaS Kayed
  • 422
  • 3
  • 9
  • This was a helpful answer. Of note, the token is a hash so not directly usable. However, it is good enough to pass on via the requests package to another website thus this is a good basis for building an application proxy. – Douglas M Jun 11 '23 at 00:41