Django & TastyPie: request.POST is empty

杀马特。学长 韩版系。学妹 提交于 2019-12-18 19:09:33

问题


I'm 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())

Anybody knows what I'm missing?

Thanks in advance.


回答1:


Starting at Django 1.5, POST does not contain non-form data anymore. They are now in request.body.

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




回答2:


I'm not a cURL expect but copying a POST request out of Chrome dev tools my --data looked as follows:

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

So it looks like you might want to change your command to:

--data item_id="1"

Side note: I can highly recommend either of the following Chrome apps for making HTTP requests:

Advanced REST client OR Dev HTTP Client

Additionally if you can make the call in a browser (form submit or such like) then in Chrome dev tools network panel you can copy the request as a cURL command (right click on it)



来源:https://stackoverflow.com/questions/16213324/django-tastypie-request-post-is-empty

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!