django:request.POST 为空

问题描述 投票:0回答:2

我有以下其余 API 端点:

def post(request, *args, **kwargs):
   print(request.POST)
   short_description = request.POST.get("short_description", "")
   long_description = request.POST.get("long_description", "")

   # rest of the code goes here

当我打电话时

response = client.post("/foo/bar/api/v1/error/1", {"short_description": "hello", "long_description": "world", format='json')

它给了我

<QueryDict: {}>

所以

short_description
long_description
都是空字符串。我怎样才能让它在 POST 中传递正确的参数?

python django rest django-rest-framework django-views
2个回答
1
投票

我认为Django中的

post
方法会将数据存储在
request.body
中,请检查一下


0
投票

根据 Django 的文档,request.POST 是

一个类似字典的对象,包含所有给定的 HTTP POST 参数,假设请求包含表单数据

请注意,要使用来自

request.POST
的解析数据填充
request.body
,提交的数据必须是表单数据。如果没有,你最好的选择可能是这样的:

import json
post_data = json.loads(request.body.decode())
© www.soinside.com 2019 - 2024. All rights reserved.