0

I want to get the value of header location in python.

When I use requests, the GET request basically headers without Location.

headers = {"content-type": "application/json", "Accept-Charset": "UTF-8"}

result = requests.get('https://forms.is.yail18.com/laptop', headers=headers, verify=False)
location = result.headers
print(location)

{'Date': 'Thu, 24 Feb 2022 14:59:48 GMT', 'Server': 'Apache', 'X-Powered-By': 'PHP/7.2.24', 'Expires': 'Thu, 19 Nov 1981 08:52:00 GMT', 'Cache-Control': 'no-store, no-cache, must-revalidate', 'Pragma': 'no-cache', 'Content-Length': '3158', 'Content-Type': 'text/html; charset=UTF-8', 'Keep-Alive': 'timeout=15, max=99', 'Connection': 'Keep-Alive'}

When I use urllib2, it has the same behavior. header without Location

f = urllib2.urlopen('https://forms.is.yail18.com/laptop')
print(f.headers)

Date: Thu, 24 Feb 2022 14:42:40 GMT
Server: Apache
X-Powered-By: PHP/7.2.24
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Content-Length: 3158
Content-Type: text/html; charset=UTF-8
Set-Cookie: form=bc32f147d42e975793f6890c7d7b3377; path=/
Connection: close

When I use curl command executed from the shell, it returns the right content.

curl -v 'https://forms.is.yail18.com/laptop'
...
skipped
...
* Mark bundle as not supporting multiuse
< HTTP/1.1 302 Found
< Date: Thu, 24 Feb 2022 14:21:50 GMT
< Server: Apache
< X-Powered-By: PHP/7.2.24
< Expires: Thu, 19 Nov 1981 08:52:00 GMT
< Cache-Control: no-store, no-cache, must-revalidate
< Pragma: no-cache
< Location: https://forms.is.yail18.com/laptop/c88a74d100985529b1644c217d2c92bf
< Content-Length: 0
< Content-Type: text/html; charset=UTF-8
< Set-Cookie: form=c0072bd1510a149f613e3b6bca3086db; path=/

So my question is, how can I get the request to return the header location in python not using shell?

Omar Abdelrazik
  • 683
  • 2
  • 9
  • 30
  • *"the GET request basically returns the html page."* No it doesn't. It returns a `Respone` object that has an attribute `text` that contain the HTML. There is also an attribute `headers`. – Klaus D. Feb 24 '22 at 14:58
  • no. The headers returned don't include `Location` – Omar Abdelrazik Feb 24 '22 at 15:00

1 Answers1

0

requests automatically follows the redirect in the response. The response object you are looking at is the answer to the second request, which does not contain a Location header. You probably want to disable this by passing allow_redirects=False to requests.get()

Chronial
  • 66,706
  • 14
  • 93
  • 99