Making HTTP request with Django and deserializing output
You would use urllib2
and json
standard modules (or, alternatively, the excellent library requests and json
):
import urllib2
import json
url = 'http://wbsapi.withings.net/[service_name]?action=[action_name]&[parameters]'
serialized_data = urllib2.urlopen(url).read()
data = json.loads(serialized_data)
If you want it on a page, you want it in a view, which you will need to associate with an url.
Your urls.py
will contain something like
from django.conf.urls import patterns, url, include
urlpatterns = patterns('',
(r'^get_data/$', 'myapp.views.get_data'),
)
And your myapp/views.py
will contain something like
from django.http import HttpResponse
import urllib2
import json
def get_data(request):
url = 'http://wbsapi.withings.net/[service_name]?action=[action_name]&[parameters]'
serialized_data = urllib2.urlopen(url).read()
data = json.loads(serialized_data)
html = "<html><body><pre>Data: %s.</pre></body></html>" % json.dumps(data, indent=2)
return HttpResponse(html)
Of course, I don't know how you wanted your data to be displayed, so I just serialized it again :)
elykl33t
Updated on June 18, 2022Comments
-
elykl33t almost 2 years
So I'm almost totally new to web development as a whole, but have been thrown into a side project using Django to pull and parse data from a web service, and am struggling to understand exactly how things work, even while looking through the Django documentation.
In Django, I have everything set up and working at a basic level (using templates, a page is displayed saying "Hello World").
Now in order to pull the data from the webservice, I need to make a request to a URL of the following format:
http://wbsapi.withings.net/[service_name]?action=[action_name]&[parameters]
In the provided PHP example, they do this using cURL, and then json_decode.
What would I do to get similar functionality out of Django? Thanks in advance!
-
elykl33t over 11 yearsWhere would I put this within the app? Would it have its own python file? Would it go in views.py? Sorry I have so many questions! Thanks!
-
Pavel Anossov over 11 yearsWhat are you trying to achieve?
-
elykl33t over 11 yearsFor now, very basic. I want to be able to send the request, get the json data, deserialize it, and display it on a page.
-
elykl33t over 11 yearsThanks very much Pavel! The overall structure of the app is starting to make a lot more sense now. The only error I'm running into right now is the page says:
Using the URLconf defined in withings.urls, Django tried these URL patterns, in this order: ^get_data/$ The current URL, , didn't match any of these.
How can I fix this? -
Pavel Anossov over 11 yearsYou're trying to request an url you didn't define. The only url available is "/get_data/". Did you forget any of the slashes?
-
elykl33t over 11 yearsI ended up figuring it out. Thanks very much for the help!
-
Kairat Kempirbaev about 7 years@Pavel Anossov is urllib2.urlopen going to block all website till roundtrip of request finishes?
-
Ahtisham over 6 years@PavelAnossov Thanks a lot sir :)