Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Python request post method but getting 401 response code

I am using Python request module post method to process json data, like so.

r = requests.post(url,data=json.dumps(payload),headers=headers)

However I am getting response[401]. Does it mean I need to first authorize it by adding login first?

like image 489
Peter Chao Avatar asked Dec 09 '22 04:12

Peter Chao


1 Answers

A 401 response, as you noticed, means that you need to authenticate in order to make the request. How you do this depends on the auth system in place, but your comment "I use HTTPBasicAuth(user, password) that give me response code of 200" suggests that it's just Basic Auth - which is easy to deal with in requests.

Anywhere you do a requests call, add the kwarg auth=(USERNAME, PASSWORD) - so for your example above, you'd do r = requests.post(url,data=json.dumps(payload),headers=headers, auth=(USERNAME, PASSWORD)) (replacing USERNAME and PASSWORD with the right values, as shown here.

like image 153
Dan Avatar answered Dec 15 '22 00:12

Dan