Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 552
Bala Avatar asked Oct 13 '11 07:10

Bala


People also ask

How do you get a json object from a RESTful web service?

First, a String URL to call the RESTful Web Service, and second, the name of the class should return with the response. So, in just one line of code, it calls the RESTful web service, parses the JSON response, and returns the Java object to you.

How do you access JSON data in Python?

It's pretty easy to load a JSON object in Python. Python has a built-in package called json, which can be used to work with JSON data. It's done by using the JSON module, which provides us with a lot of methods which among loads() and load() methods are gonna help us to read the JSON file.

How do you access REST API in Python?

REST APIs provide access to web service data through public web URLs. This URL allows you to access information about a specific GitHub user. You access data from a REST API by sending an HTTP request to a specific URL and processing the response.


2 Answers

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() 
like image 179
Mark Gemmill Avatar answered Sep 17 '22 21:09

Mark Gemmill


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

import json import urllib2 json.load(urllib2.urlopen("url")) 
like image 43
Trufa Avatar answered Sep 20 '22 21:09

Trufa