Posting JSON Data to a Django App
Overview
The goal is to create a Django app that accepts JSON data via POST requests.
External resources
- Serializing Django objects (Django documentation)
- TastyPie documentation (API package)
The Tastypie package seems best used as a simple interface between JSON and reads/writes to a database. It does do that very easily, but I was frustrated in using it to de/serialize objects and JSON POST data. - Django packages that support API creation
- How to Post JSON to Django (StackOverflow)
Testing POST data with curl
- CSRF Tokens and Rejected Requests (Django documentation)
By default Django checks for Cross Site Request Forgery (CSRF) for POST requests. This will cause a 403 response for curl requests that don't include csrf token values in the POST data.
Maybe curl isn't the correct way to test POST requests. Maybe it should be done with Django tests instead.
One way around this is to mark a view as being exempt from CSRF protection:
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
def my_view(request):
return HttpResponse('Hello world')