Django在request.body中POST数据但不能存储在变量中(Django POST data in request.body but can not store in variable)

系统教程 行业动态 更新时间:2024-06-14 16:59:47
Django在request.body中POST数据但不能存储在变量中(Django POST data in request.body but can not store in variable)

我正在向我的后端发布一些原始JSON

{ "access_token": "hU5C7so4reJOYTzPyhKPs8PWq07tb", "id": 3, "order_details": [{"meal_id": 1, "quantity": 3}, {"meal_id": 2, "quantity": 2}] }

但是,当我尝试

print (request.POST.get("access_token"))

我None收到

但是,当我这样做的时候

print (request.body)

值似乎在那里: b'{\n\t"access_token": "hU5C7so4reJOYTzPyhKPs8PWq07tb",\n\t"id": 3,\n\t",\n\t"order_details": [{"meal_id": 1, "quantity": 3}, {"meal_id": 2, "quantity": 2}]\n}'

我正在使用Postman发布数据。

我想将访问令牌存储到某个变量中,如下所示:

access_token = request.POST.get("access_token")

与帖子数据

我仍然是Django的新手,任何帮助都会很棒。

I am posting some raw JSON to my backend

{ "access_token": "hU5C7so4reJOYTzPyhKPs8PWq07tb", "id": 3, "order_details": [{"meal_id": 1, "quantity": 3}, {"meal_id": 2, "quantity": 2}] }

However when I try to

print (request.POST.get("access_token"))

I receive None

But, when I do

print (request.body)

The values seem to there: b'{\n\t"access_token": "hU5C7so4reJOYTzPyhKPs8PWq07tb",\n\t"id": 3,\n\t",\n\t"order_details": [{"meal_id": 1, "quantity": 3}, {"meal_id": 2, "quantity": 2}]\n}'

I am using Postman to post the data.

I want to store the access token into some variable like so:

access_token = request.POST.get("access_token")

with the post data

I am still fairly new to Django, any help would be great.

最满意答案

只是做一个json.loads应该工作。

import json json_data = json.loads(request.body)

Just doing a json.loads should work.

import json json_data = json.loads(request.body)

更多推荐

本文发布于:2023-04-17 09:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/53aa57e4ede1f5741c7299ae651de6f6.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   数据   body   Django   request

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!