0

I am trying to make a POST request to URL , to authenticate myself ( can't share the URL as it is for work). The authentication worked and I got the token access. However , I need to extract the token access header to use it in other function , I don't want to copy the token and paste it .

So , I tried to do this :

response = requests.post(url, data = dic,json={'key':'value'}) # dic is my username and password 
token =response.request.headers['AccessToken']

But it is still running , I did not get an error or any output ( I am using Jupyter Notebook)

response = requests.post(url, data = dic) # this one works fine but it does not meet the requirement 

Additional clarification : I am asking the user for (username and password) , then I place them into dic which I pass into the POST request.

Example of Server Response :

{"Result":{"AccessToken":"#####="},"Success":true,"Message":"","ErrorCode":""}

Is there other way to access the AccessToken header of the server response ?

Any ideas?

Thank you

Eithar
  • 97
  • 1
  • 7
  • Does this answer your question? [How to send POST request?](https://stackoverflow.com/questions/11322430/how-to-send-post-request) – baduker Oct 20 '20 at 06:16
  • @baduker I feel like you didn't read the question – OneCricketeer Oct 20 '20 at 06:16
  • @baduker , not it does not . – Eithar Oct 20 '20 at 06:18
  • So, `data` and `json` do equivalent things... Certainly you dont want both. But also accessing `response.request` seems odd if you want headers from the response (you already sent the request without any headers set) – OneCricketeer Oct 20 '20 at 06:18
  • @onecricketeer, I am sending the request with username and password as input from user (dic) then I am passing the dic in the post request .. but I want to extract certain header from the server response (AccessToken ) – Eithar Oct 20 '20 at 06:22
  • I understood that, but 1) if you did `dic.update({'key':'value'})` and `post(url, json=dic)`, that would suffice, and as mentioned, I think you're accessing the headers incorrectly, but its also not clear why that would hang. You also wouldn't get output in a cell that only assigned the response variable, so try printing anything there – OneCricketeer Oct 20 '20 at 06:29
  • @onecricketeer , I have been accessing the headers in the wrong way .. I added server response example , can you please check it out ? – Eithar Oct 20 '20 at 06:40
  • 1
    Those dont look like headers... How are you getting that output? Is that `response.text`? If so, how about `response.json()["Result"]["AccessToken"]` – OneCricketeer Oct 20 '20 at 06:43
  • @onecricketeer , Thank you so much that worked ... really appreciate it – Eithar Oct 20 '20 at 06:49

2 Answers2

0

You need to check that there is a header containing an access token in your response.headers dict. You'll probably want to look for an Authorization header, which you'll need to extract the actual token from. In the cases that I'm aware of, that header contents will be

"Authorization": "Bearer encoded-access-token-goes-here"

so you'd check

    if "Authorization" in response.headers:
        token = response.headers["Authorization"].split()[1]
James McPherson
  • 2,476
  • 1
  • 12
  • 16
0

Example of Server Response :

{"Result":{"AccessToken":"#####="},"Success":true,"Message":"","ErrorCode":""}

Since that is a response, and not a header, you want something like

token = None
try:
    token = response.json()["Result"]["AccessToken"]
except KeyError:
    print("No token found")
    print(response.text)
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245