Adding REST to Django


Question

I've got a Django application that works nicely. I'm adding REST services. I'm looking for some additional input on my REST strategy.

Here are some examples of things I'm wringing my hands over.

  • Right now, I'm using the Django-REST API with a pile of patches.
  • I'm thinking of falling back to simply writing view functions in Django that return JSON results.
  • I can also see filtering the REST requests in Apache and routing them to a separate, non-Django server instance.

Please nominate one approach per answer so we can vote them up or down.

1
50
8/7/2012 2:36:01 PM

Accepted Answer

I'm thinking of falling back to simply writing view functions in Django that return JSON results.

  • Explicit
  • Portable to other frameworks
  • Doesn't require patching Django
59
11/21/2008 1:38:49 PM

Please note that REST does not just mean JSON results. REST essentially means exposing a resource-oriented API over native but full-fledged HTTP. I am not an expert on REST, but here are a few of the things Rails is doing.

  • URLs should be good, simple names for resources
  • Use the right HTTP methods
    • HEAD, GET, POST, PUT, and DELETE
    • Optionally with an override (form parameter '_method' will override HTTP request-method)
  • Support content-type negotiation via Accept request-header
    • Optionally with an override (filename extension in the URL will override MIME-type in the Accept request-header)
    • Available content types should include XML, XHTML, HTML, JSON, YAML, and many others as appropriate

For example, to get the native HTTP support going, the server should respond to

GET /account/profile HTTP/1.1
Host: example.com
Accept: application/json

as it would respond to

GET /account/profile.json HTTP/1.1
Host: example.com

And it should respond to

PUT /account/profile HTTP/1.1
Host: example.com

var=value

as it would respond to

POST /account/profile HTTP/1.1
Host: example.com

_method=PUT&var=value

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon