86

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.

2
  • This may help you stackoverflow.com/questions/713847/… Commented Oct 13, 2011 at 7:14
  • 2
    I'm not looking fro " Python-based REST frameworks". I want to use RESTful service provided by some java server in python. Thanks anyway. Commented Oct 13, 2011 at 7:18

5 Answers 5

127

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()
Sign up to request clarification or add additional context in comments.

2 Comments

If you're missing the requests module, simply do: pip install requests. More info and docs here
here why my json response become with u before the key, value pair? {u'status': u'FINISHED', u'startTime': u'2016-11-08T15:32:33.241Z', u'jobId': u'f9d71eaa-d439-4a39-a258-54220b14f1b8', u'context': u'sql-context', u'duration': u'0.061 secs'}
77

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

import json
import urllib2
json.load(urllib2.urlopen("url"))

9 Comments

this would work if there are no credential required to pass. But I get this "urllib2.HTTPError: HTTP Error 401: Unauthorized" error
Where are you trying to download from?
I need to use Kerberos authentication. Sorry, I forgot to mention in question.
@BalamuruganK what OS are you using?
I'm using unix. trying with kerberos lib to get token to pass it to httpConnection.putheader('Authorization', ?)
|
28

You basically need to make a HTTP request to the service, and then parse the body of the response. I like to use httplib2 for it:

import httplib2 as http
import json

try:
    from urlparse import urlparse
except ImportError:
    from urllib.parse import urlparse

headers = {
    'Accept': 'application/json',
    'Content-Type': 'application/json; charset=UTF-8'
}

uri = 'http://yourservice.com'
path = '/path/to/resource/'

target = urlparse(uri+path)
method = 'GET'
body = ''

h = http.Http()

# If you need authentication some example:
if auth:
    h.add_credentials(auth.user, auth.password)

response, content = h.request(
        target.geturl(),
        method,
        body,
        headers)

# assume that content is a json reply
# parse content with the json module
data = json.loads(content)

Comments

10

If you desire to use Python 3, you can use the following:

import json
import urllib.request
req = urllib.request.Request('url')
with urllib.request.urlopen(req) as response:
    result = json.loads(response.readall().decode('utf-8'))

1 Comment

How does this use kerberos for authentication?
3

Well first of all I think rolling out your own solution for this all you need is urllib2 or httplib2 . Anyways in case you do require a generic REST client check this out .

https://github.com/scastillo/siesta

However i think the feature set of the library will not work for most web services because they shall probably using oauth etc .. . Also I don't like the fact that it is written over httplib which is a pain as compared to httplib2 still should work for you if you don't have to handle a lot of redirections etc ..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.