So, I want to use the excellent Kenneth comment module . This problem occurred while trying to use the Freebase API .
Basically, their API looks like this:
https://www.googleapis.com/freebase/v1/mqlread?query=...
as a request, they expect a JSON object, here is one that will return a list of wines with their country and a percentage of alcohol :
[{
"country": null,
"name": null,
"percentage_alcohol": null,
"percentage_alcohol>": 0,
"type": "/food/wine"
}]
Of course, we will have to avoid this before passing it to the URL, so the actual request will look like this:
fullurl = 'https://www.googleapis.com/freebase/v1/mqlread?query=%5B%7B%22percentage_alcohol%3E%22%3A+0%2C+%22country%22%3A+null%2C+%22type%22%3A+%22%2Ffood%2Fwine%22%2C+%22name%22%3A+null%2C+%22percentage_alcohol%22%3A+null%7D%5D'
Now,
r = requests.get(fullurl)
print r.status_code
>>> 400
because the site claims to be unable to parse the request.
r2 = urllib2.urlopen(fullurl)
print r2.getcode()
>>> 200
No problem, I get the correct refund. Interesting that
# This is the url of our requests.get request
print urllib2.urlopen(r.url).getcode()
>>> 200
Why? Am I using the module incorrectly? Or is it a mistake in requests?