skip to Main Content

I am using python requests and doing a post

import requests  
response = requests.post('https://petdogs.net/search/?input=abcdefgh', 
headers=HEADERS, 
allow_redirects=False)

print(response.headers)

These are the values in response header that I can see in developer tools in browser and I want to get the value for location

content-language: en-gb
content-length: 0
content-type: text/html; charset=utf-8
date: Wed, 07 Jul 2021 17:44:52 GMT
location: /product/id=12345/
server: nginx/1.14.0 (Ubuntu)
vary: Accept-Language, Cookie, Origin
x-content-type-options: nosniff
x-frame-options: DENY

but when I do print(response.headers) I see only this

{'Server': 'nginx/1.14.0 (Ubuntu)', 'Date': 'Wed, 07 Jul 2021 18:23:45 GMT',
'Content-Type': 'text/html', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive',
 'X-Frame-Options': 'DENY', 'Vary': 'Accept-Language, Origin', 
'Content-Language': 'en', 'X-Content-Type-Options': 'nosniff', 'Content-Encoding': 'gzip'}

and location is missing

I saw a few answers that talked about

'Access-Control-Expose-Headers': 'Location'

but I do not know if it is correct and/or how to use it correctly.

I have also tried using urllib

import urllib.request as urllib2
>>> f = urllib2.urlopen('https://petdogs.net/search/?input=abcdefgh')
>>> print(f.headers)

but this responds with

Server: nginx/1.14.0 (Ubuntu)
Date: Thu, 08 Jul 2021 11:12:58 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 128053
Connection: close
X-Frame-Options: DENY
Vary: Cookie, Accept-Language, Origin
Content-Language: en
X-Content-Type-Options: nosniff
Set-Cookie: csrftoken=xxxxxx; expires=Thu, 07 Jul 2022 11:12:57 GMT; Max-Age=31449600; Path=/; SameSite=Lax
Set-Cookie: sessionid=bbbbbb; expires=Thu, 22 Jul 2021 11:12:57 GMT; HttpOnly; Max-Age=1209600; Path=/; SameSite=Lax

How do I get the value for location?

2

Answers


  1. Using urllib, its straight forward.enter image description here

    from urllib import request, parse
    
    def create_post():
        payload = {'title':'Pyton Requests','body':'Requests are qwesome','userId':1}
        data = parse.urlencode(payload).encode()
        response = request.Request('https://jsonplaceholder.typicode.com/posts', data) 
        resp = request.urlopen(response)
        print(resp.headers['Location'])
    
    
    def main():
        # """ Main entry point of the app """
        # print("hello world")
        create_post()
    
    
    if __name__ == "__main__":
        """ This is executed when run from the command line """
        main()
    
    Login or Signup to reply.
  2. The solution is simple.
    This "location" response header is a rediction.
    And the python request module is trying to make things more easy
    so it tries to automatically redicit you.
    So you can just add the "allow_redirects" parameter and set it to False.

    (The request method shouldn’t matter)

    Example:

    import requests
    
    res = requests.get("example.com", allow_redirects=False)
    print(res.headers)
    

    Output:

    {'Content-Length': '0', 'location': 'your stuff here'}
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search