Django&tastype:request.POST为空

2024-06-01 01:10:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着用curl写一篇文章:

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

但是,request.POST始终为空。

下面是我的模型资源代码:

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

有人知道我错过了什么吗?

提前谢谢。


Tags: selfidjsondatareturnapplicationrequestdef
2条回答

我不是cURL expect,但从Chrome开发工具中复制POST请求我的--data如下所示:

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

因此,您可能希望将命令更改为:

--data item_id="1"

旁注: 我可以强烈推荐以下任何一个Chrome应用程序来发出HTTP请求:

Advanced REST clientDev HTTP Client

此外,如果您可以在浏览器中进行调用(表单提交等),那么在Chrome开发工具网络面板中,您可以将请求复制为cURL命令(右键单击它)

从Django 1.5开始,POST不再包含非表单数据。他们现在在请求中。

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

相关问题 更多 >