Fix: Python requests.get(url) times out but works in browser

If Python requests.get fails with a timeout and the same URL works in the browser then you will need to simulate the browser request as is while invoking requests.get.  
Usually, this will get resolved if you put the "user-agent" header, which tells the server which browser is requesting. 


session = requests.Session()

headers = {
           'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36',          
      }

response = session.request('GET', url, headers=headers, allow_redirects=False)
If this does not resolve the issue then launch the Developer Tools in Chrome and launch the same URL in chrome. Now on Developer Tools check the network tab which shows the requests made by the browser and their details. Copy all the request headers from there and put it into the headers map above and try again.
This time you should not face the issue.

No comments:

Post a Comment

Golang: Http POST Request with JSON Body example

Go standard library comes with "net/http" package which has excellent support for HTTP Client and Server.   In order to post JSON ...