Calling a REST API from django view

Posted on

Question :

Calling a REST API from django view

Is there any way to make a RESTful api call from django view?

I am trying to pass header and parameters along a url from the django views. I am googling from half an hour but could not find anything interesting.

Any help would be appreciated

Answer #1:

Yes of course there is. You could use urllib2.urlopen but I prefer requests.

import requests

def my_django_view(request):
    if request.method == 'POST':
        r ='', params=request.POST)
        r = requests.get('', params=request.GET)
    if r.status_code == 200:
        return HttpResponse('Yay, it worked')
    return HttpResponse('Could not save data')

The requests library is a very simple API over the top of urllib3, everything you need to know about making a request using it can be found here.

Answered By: aychedee

Leave a Reply

Your email address will not be published. Required fields are marked *