首页 > 解决方案 > Django 请求反序列化

问题描述

我正在尝试反序列化 request.body:

from django.contrib.auth.models import User
from django.http import HttpResponse, HttpRequest
from django.http import HttpResponseRedirect
from django.shortcuts import render
import json



def register(request):
    if request.method == 'POST':
        if len(request.body) > 0 and len(request.FILES) == 0:
            data = json.loads(request.body)

我在最后一行得到了 JSONDecodeError 。Python 3.6.4(无需解码,应该接受字节),Django 2.0.7。当我尝试打印 request.body 时,我得到:

b'username=1&password=1&email=and%40gm.co&csrfmiddlewaretoken=1iv975vw86CmmcDQM7IbpWxKsRn3J0RguI4ek5qKmTSfnyCRmu3wy7tcG0f36Cmr'

如果可能有帮助,还可以输入(请求):

<class 'django.core.handlers.wsgi.WSGIRequest'>

我究竟做错了什么?

标签: pythonjsondjango

解决方案


推荐阅读