Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get Django to make a RESTful call?

Tags:

rest

http

django

Note: I'm not trying to provide a RESTful api, I'm trying to call one.

def index( request, *args, **kwargs ):
    context = {}
    context['some_json'] = make_remote_api_call( "http://exampl.com/objects/" )

    return render_to_response( 'index.html', context, context_instance=RequestContext(request) )

Or is this just ridiculous and I should make the client always do it? Basically, I'm curious as to how to properly divide my website so that different servers (internal protected ones) can provide different subsets of info.

like image 888
Kurt Avatar asked Oct 08 '11 21:10

Kurt


People also ask

Can Django make API calls?

Yes, if you have a single threaded, single worker Django app and you spend 2 seconds making an http request.

How do I create a REST API call?

Step #1 – Enter the URL of the API in the textbox of the tool. Step #2 – Select the HTTP method used for this API (GET, POST, PATCH, etc). Step #3 – Enter any headers if they are required in the Headers textbox. Step #4 – Pass the request body of the API in a key-value pair.


1 Answers

Use the requests library. Here is some discussion on it and I did a talk on "Consuming Web APIs with Python" at PyOhio that you might find interesting or helpful

  • Video.
  • Slides.
  • Code.
like image 81
Issac Kelly Avatar answered Oct 22 '22 10:10

Issac Kelly