0

What would cause requests.get to timeout, when the url works in the browser?

I've seen the response at requests.get returns 403 while the same url works in browser which talks about masking the User-Agent, but my issue is that I get a TimeoutError, not that it's forbidden. I tried it anyway, and it didn't help.

import requests

def onemap_geocode(postalcode):
    header = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
    onemap_request = 'https://developers.onemap.sg/commonapi/search?searchVal=' + str(postalcode) + '&returnGeom=Y&getAddrDetails=N'
    print(onemap_request)
    response = requests.get(onemap_request,headers=header)
    print('response obtained')
    response_json = response.json
    lat = response_json['results'][0]['LATITUDE']
    lon = response_json['results'][0]['LONGITUDE']

    return lat + ', ' + lon

onemap_geocode(178880)
Community
  • 1
  • 1
  • 1
    I tried and don't get timeout. But you should update the line `esponse_json = response.json` by `response_json = response.json()`, because it can not works like this. – Arount Jan 31 '17 at 10:07
  • Well, the problem might be on your side, I got the result almost immediately – Shane Jan 31 '17 at 10:07
  • I managed to get a result on my personal pc on my own network, so I'm beginning to suspect that the network at work is blocking the response somehow. The tech support here isn't great, however, so I need to identify what it is that is causing it to fail when I make the API call like this, but it works in the browser, even with the header masking. Any ideas? – Friedemann Ang Feb 06 '17 at 01:18

2 Answers2

0

Use a large number in timeout:

response = requests.get(onemap_request,headers=header,timeout=100)
Mithilesh Gupta
  • 2,800
  • 1
  • 17
  • 17
0

response_json = response.json

You are missing the parenthesis when calling the response.json method. This line should be changed to response_json = response.json().

Other than that your code works as expected.

DeepSpace
  • 78,697
  • 11
  • 109
  • 154