How do I get JSON data from RESTful service using Python?


Question

Is there any standard way of getting JSON data from RESTful service using Python?

I need to use kerberos for authentication.

some snippet would help.

1
78
10/13/2011 3:51:54 PM

Accepted Answer

Something like this should work unless I'm missing the point:

import json
import urllib2
json.load(urllib2.urlopen("url"))
77
10/13/2011 8:47:08 AM

I would give the requests library a try for this. Essentially just a much easier to use wrapper around the standard library modules (i.e. urllib2, httplib2, etc.) you would use for the same thing. For example, to fetch json data from a url that requires basic authentication would look like this:

import requests

response = requests.get('http://thedataishere.com',
                         auth=('user', 'password'))
data = response.json()

For kerberos authentication the requests project has the reqests-kerberos library which provides a kerberos authentication class that you can use with requests:

import requests
from requests_kerberos import HTTPKerberosAuth

response = requests.get('http://thedataishere.com',
                         auth=HTTPKerberosAuth())
data = response.json()

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon