I’m trying to call the NYT events api with urllib2 but I’m receiving a 596 error. If I construct the url myself, there is no problem, but if I call urlopen with the data instead, I receive the 596 error. What’s going on? The 596 error seems to be undocumented, so it doesn’t help.
>>> data = urllib.urlencode({'api-key': os.environ['NYT_EVENT_LISTING_API_KEY']})
>>> resp = urllib2.urlopen('?'.join([url,data]))
>>> resp = urllib2.urlopen(url, data)
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 400, in open
response = meth(req, response)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 513, in http_response
'http', request, response, code, msg, hdrs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 438, in error
return self._call_chain(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 372, in _call_chain
result = func(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 521, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 596:
@Thomas is right, you are using
GETin your first request which will construct the URL to something like this:However, your second call to
urllib2.urlopensends thedataas aPOSTrequest to this URLinstead, which gives you 596 service not found error.
Now,
urllib2is notorious for its non-intuitive API and documentation, you may consider usingRequestsinstead:This way,
GETrequests andPOSTrequests are a lot easier to distinguish, url and parameters are separated as well.