Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a JSON object to an url with requests

So, I want to use Kenneth' excellent requests module. Stumbled up this problem while trying to use the Freebase API.

Basically, their API looks like that:

https://www.googleapis.com/freebase/v1/mqlread?query=...

as a query, they expect a JSON object, here's one that will return a list of wines with their country and percentage of alcohol:

[{
  "country":       null,
  "name":          null,
  "percentage_alcohol": null,
  "percentage_alcohol>": 0,
  "type":          "/food/wine"
}]​

Of course, we'll have to escape the hell out of this before passing it to an URL, so the actual query 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 it couldn't parse the query.

r2 = urllib2.urlopen(fullurl)
print r2.getcode()
>>> 200

No problem here, I get a proper return. Interestingly,

# This is the url of our requests.get request
print urllib2.urlopen(r.url).getcode() 
>>> 200

Why? Am I using the module wrong? Or is it a bug in requests?

like image 738
Manuel Ebert Avatar asked May 08 '12 17:05

Manuel Ebert


People also ask

Can we pass JSON object GET request?

To get JSON from a REST API endpoint, you must send an HTTP GET request and pass the "Accept: application/json" request header to the server, which will tell the server that the client expects JSON in response.

How do you pass a JSON object into a URL in Python?

To post a JSON to the server using Python Requests Library, call the requests. post() method and pass the target URL as the first parameter and the JSON data with the json= parameter. The json= parameter takes a dictionary and automatically converts it to a JSON string.


1 Answers

It works for me. Here's what I did:

>>> params = [{"country": None,
...            "name": None,
...            "percentage_alcohol": None,
...            "percentage_alcohol>": 0,
...            "type": "/food/wine"
...          }]
>>> import json
>>> params_json = json.dumps(params)

>>> import requests
>>> url = "https://www.googleapis.com/freebase/v1/mqlread?query=%s"
>>> r = requests.get(url % params_json)
>>> r.status_code
200

>>> content_json = json.loads(r.content)
>>> import pprint
>>> pprint.pprint(content_json)
{u'result': [{u'country': u'New Zealand',
              u'name': u'2003 Cloudy Bay Sauvignon Blanc',
              u'percentage_alcohol': 13.5,
              u'type': u'/food/wine'},
             {u'country': u'France',
              u'name': u'G.H. Mumm Cordon Rouge Brut',
              u'percentage_alcohol': 12.0,
              u'type': u'/food/wine'},
....

I cut the rest off for brevity. There are 100 results. requests.__version__ == '0.10.6'

like image 70
jterrace Avatar answered Oct 19 '22 23:10

jterrace