Django & TastyPie: request.POST is empty

I am trying to do a POST using curl:

curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"item_id": "1"}' http://www.mylocal.com:8000/api/1/bookmarks/ 

However, request.POST is always empty.

Below is my ModelResource code:

 class BookmarkResource(ModelResource): class Meta: queryset = Bookmark.objects.all() resource_name = 'bookmarks' fields = ['id', 'tags'] allowed_methods = ['get', 'post', 'delete', 'put'] always_return_data = True authorization= Authorization() include_resource_uri = False def determine_format(self, request): return "application/json" def obj_create(self, bundle, **kwargs): request = bundle.request try: payload = simplejson.loads(request.POST.keys()[0]) except: payload = simplejson.loads(request.POST.keys()) 

Does anyone know what I am missing?

Thanks in advance.

+8
django tastypie
source share
2 answers

Starting with Django 1.5, POST no longer contains data other than the form. Now they are in request.body.

https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.POST

+29
source share

I do not expect cURL, but copying the POST request from Chrome dev tools, my --data looked like this:

--data "foo=bar&bar=foo"

So it looks like you can change your command to:

--data item_id="1"

Note: I can highly recommend one of the following Chrome applications for making HTTP requests:

Advanced REST Client OR Dev HTTP Client

In addition, if you can make a call in the browser (send a form or the like), then in the Chrome Developer Network toolbar, you can copy the request as a cURL command (right-click on it)

0
source share

All Articles