Unable to fetch pdf with urllib2 : HTTP Error 400: Bad Request

We found that when trying to access, view, download files using urllib2 we occasionally came across the following error (<class ‘urllib2.HTTPError’>: HTTP Error 400: Bad Request)

In most cases when this happened we were able to access the files using httplib instead.

for example

instead of using urllib2.urlopen(geturl).read()

We would use

>>>conn = httplib.HTTPConnection(“serverAnd:portToAccess”)
>>> this = conn.putrequest(“GET”, “fileUrlGoesHere”)
>>> conn.endheaders()
>>> r = conn.getresponse()

2 thoughts on “Unable to fetch pdf with urllib2 : HTTP Error 400: Bad Request

Leave a comment