[英]access data from request body django
I am using django v2.2.4
and need to access request body data. 我正在使用
django v2.2.4
,需要访问请求正文数据。
Here's my code: 这是我的代码:
@api_view(['POST'])
@renderer_classes((JSONRenderer,))
def index(request):
if request.method == 'POST':
results= []
data = JSONParser().parse(request)
serializer = ScrapeSerializer(data=data)
if serializer.is_valid():
url = request.data.url
#url = request.POST.get('url')
But I get this error: 但是我得到这个错误:
RawPostDataException at /scrape/
You cannot access body after reading from request's data stream
Here's the request body: 这是请求正文:
{
"url": "xyz.com"
}
How can I access the request body? 如何访问请求正文?
I have found this SO post that related to this issue, Exception: You cannot access body after reading from request's data stream 我发现与此问题相关的SO帖子, 例外:从请求的数据流中读取后无法访问正文
Anyway use request.data
instead of request.body
in DRF 无论如何在DRF中使用
request.data
而不是request.body
@api_view(['POST'])
@renderer_classes((JSONRenderer,))
def index(request):
if request.method == 'POST':
results = []
serializer = ScrapeSerializer(data=request.data)
if serializer.is_valid():
url = request.data["url"]
The request.data
returns the parsed content of the request body and it will be dict like object , hence the dot operation ( request.data.url
) won't works here. request.data
返回请求正文的解析内容,它将像对象一样dict ,因此点操作( request.data.url
)在这里不起作用。
要访问POST请求的请求正文,您可以通过url = request.POST.get("url")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.