Posting JSON Data to a Django App: Difference between revisions

From Littledamien Wiki
Jump to navigation Jump to search
 
Line 5: Line 5:
== External resources ==
== External resources ==


* [https://docs.djangoproject.com/en/dev/topics/serialization/ Serializing Django objects] (Django documentation)
* [http://django-tastypie.readthedocs.org/en/latest/ TastyPie documentation] (API package)<br />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.   
* [http://django-tastypie.readthedocs.org/en/latest/ TastyPie documentation] (API package)<br />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.   
* [http://www.djangopackages.com/grids/g/api/ Django packages that support API creation]
* [http://www.djangopackages.com/grids/g/api/ Django packages that support API creation]
* [http://stackoverflow.com/questions/10128900/how-to-post-json-to-django How to Post JSON to Django] (StackOverflow)
* [http://stackoverflow.com/questions/10128900/how-to-post-json-to-django How to Post JSON to Django] (StackOverflow)
== Testing POST data with curl ==
* [https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#rejected-requests 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:
<syntaxhighlight lang="python">
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
def my_view(request):
    return HttpResponse('Hello world')
</syntaxhighlight>


[[Category:Django]] [[Category:Python]] [[Category:Web Development]]
[[Category:Django]] [[Category:Python]] [[Category:Web Development]]

Latest revision as of 16:36, 22 February 2013

Overview[edit]

The goal is to create a Django app that accepts JSON data via POST requests.

External resources[edit]

Testing POST data with curl[edit]

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')