5

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?

7
  • 1
    Can you add your authorization in your code? Commented Feb 3, 2015 at 18:28
  • without more info it is impossible to answer Commented Feb 3, 2015 at 18:29
  • 401 means you need to authorize ... what part dont you understan ... what api are you consuming ... Commented Feb 3, 2015 at 18:32
  • 1
    sorry!, I first did response = requests.get('bla.bla.bla.bla, Commented Feb 3, 2015 at 18:37
  • well there you go ... bla.blah.blah Im sure is not the correct address... also you typically need to open the authorization url in a web browser that redirects the autorization token back to a specified url Commented Feb 3, 2015 at 18:40

1 Answer 1

15

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I can access the url using my explorer, but I dont know what my authentication is. How can I use "requests" to get url content by leveraging the explorer in my computer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.