gpt4 book ai didi

python - django 中间件使发布请求为空

转载 作者:行者123 更新时间:2023-11-28 17:29:32 30 4
gpt4 key购买 nike

如果我使用中间件,我发送的发布数据将被捕获为空白,如果我从 settings.py 文件中删除中间件,它会返回发布数据。我究竟做错了什么。

enter image description here

这是我删除中间件时的正确结果

enter image description here

这是中间件代码:这里的缩进可能是错误的

class CheckAuthorization(object):
def process_request(self, request):
getKey = request.POST.get('authKey')
token = request.POST.get('token')
full_path = request.get_full_path()
if getKey is not None and getKey != '' and full_path != "/users/splash/":
try:
auth = TblAutherization.objects.get(secret_key = request.POST.get('authKey'))
except TblAutherization.DoesNotExist:
response = JsonResponse({'Status':'Error','Response code': 107,'Message':full_path})
return HttpResponse(response, content_type='application/json')
else:
return None
if token is not None and token != '' and 'settoken' in request.session and full_path != "/users/splash/":
try:
auth = TblLoginAuth.objects.get(token = request.POST.get('token'))
except TblLoginAuth.DoesNotExist:
response = JsonResponse({'Status':'Error','Response code': 107,'Message':'Invalid Request'})
return HttpResponse(response, content_type='application/json')
else:
return None

views.py

def splash(request):
if request.method == 'POST':
try:
appId = request.POST.get('appId')
except:
return Response({'Status':appId})

gcmToken = request.POST.get('gcmToken')
deviceType = request.POST.get('deviceType')
userId = request.POST.get('userId')
loginStatus = request.POST.get('loginStatus')
appType=request.POST.get('appType')
return Response({'appId':appId,'gcmToken':gcmToken,'deviceType':deviceType})
else:
return Response({'appId':appId,'gcmToken':gcmToken,'deviceType':deviceType})

它甚至没有进入,除非我使用 try except

最佳答案

我在实现日志记录中间件时遇到了同样的问题。如果您尝试在中间件中访问它,Django 似乎会从 POST dict 中删除所有数据。不知道这是错误还是功能,但我提出了临时解决方案:您可以使用 request.body 获取数据。

body = request.body.decode('utf-8')  # in python 3 json.loads only accepts unicode strings 
body = json.loads(body)
content = body['content']

关于python - django 中间件使发布请求为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35474259/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com