简体   繁体   English

Django通过cURL获取发布的POST数据

[英]Django get POST data posted via cURL

I'm sending data using cURL like this:- 我正在使用cURL发送数据,如下所示:

curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" -d "{
  \"social_id\": \"string\",
  \"social_source\": \"FB/Gmail\",
  \"access_token\": \"string\"
}"

But I'm not able to get this data in my views:- 但我无法在我的视图中获取此数据:-

Here is my view:- 这是我的看法:

class UserCreate(View):

    @method_decorator(csrf_exempt)
    def dispatch(self, request, *args, **kwargs):
        return super(UserCreate, self).dispatch(request, *args, **kwargs)

    def post(self, request):
        print request.POST['social_id']

I've tried request.data as well. 我也尝试过request.data What wrong am I doing? 我在做什么错?

If you are posting a JSON blob to the server then you will not have any POST parameters available - those are only populated when you submit data in the application/x-www-form-urlencoded format (eg, using a HTML form). 如果要向服务器发布JSON Blob,则将没有可用的POST参数-仅当您以application/x-www-form-urlencoded格式(例如,使用HTML表单)提交数据时才填充这些参数。

You can access your JSON data as follows (from inside a view function): 您可以按以下方式(从视图函数内部)访问JSON数据:

import json
data = json.loads(request.body)
# Data is now a python dict, e.g.,
print data['social_id']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM