Home > front end >  How to extract an API token from a dictionary and store it in a variable?
How to extract an API token from a dictionary and store it in a variable?

Time:05-27

I'm using Python 3.9 and using http.client to authenticate and generate a token. When I send the request, I get this response:

{"token":"eyJhbGciOiJ","user":{"id":99,"username":"[email protected]","tenantUuid":"-888-888-52558-878745"}

How can I extract the token part and store it in a variable that I can use later on?

CodePudding user response:

Assuming data holds the dictionary, you could do:

token = data["token"]

CodePudding user response:

For consistency, if the token is missing you should use a get statement to avoid a potential exception. get accepts a default value if no 'token' key is found.

data = myRequestFn(inputs, here)
token = data.get('token','default_variable_here')
  • Related