Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的字节输入不正确(不能转换为JSON)_Python_Json_Django - Fatal编程技术网

Python中的字节输入不正确(不能转换为JSON)

Python中的字节输入不正确(不能转换为JSON),python,json,django,Python,Json,Django,基本上,我有一个API端点,如果您向它发出POST请求,它将被调用。问题是出于某种原因,我无法将字节转换为JSON以便访问数据 我的代码: @api_view(['POST']) def create_user(request): """ POST = Create user. """ # Check that a username with this email doesn't already exist try: data = {} p

基本上,我有一个API端点,如果您向它发出POST请求,它将被调用。问题是出于某种原因,我无法将字节转换为JSON以便访问数据

我的代码:

@api_view(['POST'])
def create_user(request):
    """ POST = Create user. """
    # Check that a username with this email doesn't already exist
    try:
        data = {}
        print("IS IT WORKING...?")
        print(type(request.body))
        print(request.body)
        # Use double quotes to make it valid JSON
        my_json = request.body.decode('utf8').replace("'", '"')
        print("MY_JSON:")
        print(my_json)
        data = json.loads(my_json)
        print("DATA:")
        print(data)
        s = json.dumps(data, indent=4, sort_keys=True)
        print("s:")
        print(s)
    except User.DoesNotExist:
        print("PLS WORK ON CONSOLE")
    return Response(status=status.HTTP_409_CONFLICT)
我试图使用邮递员向我的路径
用户/create/
发出POST请求,但当我打印
request.body
以查看我的POST请求的内容时,它的格式不正确,带有大量随机数字和破折号。这使我无法将其转换为JSON。这是一个带有
电子邮件
密码
字段的简单POST请求

这就是奇怪的格式:

这就是“json”在我解码并用双引号转换后的样子(注意奇怪的破折号和数字):

之后,由于输入格式不正确,导致
data=json.loads(my_json)
出错

错误消息:

Internal Server Error: /users/create/
Traceback (most recent call last):
  File "/Users/mightu/Desktop/jason_app/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Users/mightu/Desktop/jason_app/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/mightu/Desktop/jason_app/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/mightu/Desktop/jason_app/env/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/mightu/Desktop/jason_app/env/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/mightu/Desktop/jason_app/env/lib/python3.7/site-packages/rest_framework/views.py", line 505, in dispatch
    response = self.handle_exception(exc)
  File "/Users/mightu/Desktop/jason_app/env/lib/python3.7/site-packages/rest_framework/views.py", line 465, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/Users/mightu/Desktop/jason_app/env/lib/python3.7/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception
    raise exc
  File "/Users/mightu/Desktop/jason_app/env/lib/python3.7/site-packages/rest_framework/views.py", line 502, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/mightu/Desktop/jason_app/env/lib/python3.7/site-packages/rest_framework/decorators.py", line 50, in handler
    return func(*args, **kwargs)
  File "/Users/mightu/Desktop/jason_app/users/views.py", line 38, in create_user
    data = json.loads(my_json)
  File "/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

我已经使用这些SO帖子(,)让我了解了我现在的处境(意识到我的输入是错误的!)。

编辑:好的,我找到了解决方案。在Postman中,您可以将数据发送为
表单数据
,也可以发送为
原始数据
。我当时使用的是
表单数据
,我想这会毁掉一切,并产生所有奇怪的------和整数。我切换到
raw
,直接输入JSON格式,我的代码现在可以工作了!全部!但我完全不明白为什么。我只是有一种预感,我发送数据的方法根本不正确。有人能解释一下吗

您好,如果这个问题有错误(打字错误或简单错误),请删除它。如果没有,但您有答案,请将您的答案作为您自己问题的答案发布,并用复选标记选择。谢谢,我会这样做(您是对的)。:)我只能在两天后接受我自己的答案!您可以尝试将POST请求上的
Content-Type
头设置为
application/json;字符集=utf-8
。这应该正确编码json。