1

I am using python requests library for a POST request and I expect a return message with an empty payload. I am interested in the headers of the returned message, specifically the 'Location' attribute. I tried the following code:

response=requests.request(method='POST', url=url, headers={'Content-Type':'application/json'}, data=data)
print response.headers ##Displays a case-insensitve map
print response.headers['Location'] ##blows up

Strangely the 'Location' attribute is missing in the headers map. If I try the same POST request on postman, I do get a valid Location attribute. Has anyone else seen this? Is this a bug in the requests library?

1
  • I think requests follows redirection, so it actually goes to that location IIRC Commented Nov 21, 2013 at 19:42

2 Answers 2

4

Sounds like everything's working as expected? Check your response.history

From the Requests documentation:

Requests will automatically perform location redirection for all verbs except HEAD.

>>> r = requests.get('http://github.com')
>>> r.url
'https://github.com/'
>>> r.status_code
200
>>> r.history
[<Response [301]>]

From the HTTP Location page on wikipedia:

The HTTP Location header field is returned in responses from an HTTP server under two circumstances:

  • To ask a web browser to load a different web page. In this circumstance, the Location header should be sent with an HTTP status code of 3xx. It is passed as part of the response by a web server when the requested URI has:
    • Moved temporarily, or
    • Moved permanently
  • To provide information about the location of a newly-created resource. In this circumstance, the Location header should be sent with an HTTP status code of 201 or 202.1
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Setting allow_redirects to False did it. I should have looked into the documentation more thoroughly
2

The requests library follows redirections automatically.

To take a look at the redirections, look at the history of the requests. More details in the docs.

Or you pass the extra allow_redirects=False parameter when making the request.

Comments

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.