Python JSONDecodeError期望值:第1行第1列(字符0)

Python JSONDecodeError期望值:第1行第1列(字符0),python,django,Python,Django,我有一个项目,我正在工作,我需要,我正试图发送一个json请求。我收到一个错误,我不知道它对我试图发送的请求意味着什么 错误如下: JSONDecodeError at /setup_profile/ Expecting value: line 1 column 1 (char 0) Request Method: POST Request URL: http://127.0.0.1:8000/setup_profile/ Django Version: 1.8.6 Exception T

我有一个项目,我正在工作,我需要,我正试图发送一个json请求。我收到一个错误,我不知道它对我试图发送的请求意味着什么

错误如下:

JSONDecodeError at /setup_profile/
Expecting value: line 1 column 1 (char 0)
Request Method: POST
Request URL:    http://127.0.0.1:8000/setup_profile/
Django Version: 1.8.6
Exception Type: JSONDecodeError
Exception Value:    
Expecting value: line 1 column 1 (char 0)
Exception Location: C:\Users\OmarJandali\AppData\Local\Programs\Python\Python36\lib\site-packages\simplejson\scanner.py in _scan_once, line 118
Python Executable:  C:\Users\OmarJandali\AppData\Local\Programs\Python\Python36\python.exe
Python Version: 3.6.1
Python Path:    
['C:\\Users\\OmarJandali\\Desktop\\opentab\\opentab',
 'C:\\Users\\OmarJandali\\AppData\\Local\\Programs\\Python\\Python36\\python36.zip',
 'C:\\Users\\OmarJandali\\AppData\\Local\\Programs\\Python\\Python36\\DLLs',
 'C:\\Users\\OmarJandali\\AppData\\Local\\Programs\\Python\\Python36\\lib',
 'C:\\Users\\OmarJandali\\AppData\\Local\\Programs\\Python\\Python36',
 'C:\\Users\\OmarJandali\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages']
以下是我正在尝试发送的请求:

def createUserSynapse(request):
    url = 'http://uat-api.synapsefi.com'
    headers = {
        'X-SP-GATEWAY' : 'client_id_asdfeavea561va9685e1gre5ara|client_secret_4651av5sa1edgvawegv1a6we1v5a6s51gv',
        'X-SP-USER-IP' : '127.0.0.1',
        'X-SP-USER' : '| ge85a41v8e16v1a618gea164g65',
        'Contant-Type' : 'application/json',
    }
    payload = {
        "logins":[
            {
                "email":"test@test.com",
            }
        ],
        "phone_numbers":[
            "123.456.7890",
            "test@test.com",
        ],
        "legal_names":[
            "Test name",
        ],
        "extras":{
            "supp_id":"asdfe515641e56wg",
            "cip_tag":12,
            "is_business":False,
        }
    }
    print(url)
    print(headers)
    print(payload)
    call = requests.post(url, json=payload, headers=headers)
    # response = json.loads(call.text)
    call = call.json()
    print (call)
    print(call.content)
    return render(request, 'tabs/create_user_synapse.html', call)
这是回溯,我认为错误将出现在call=call.json()命令所在的位置,或者实际响应所在的位置

Traceback Switch to copy-and-paste view

C:\Users\OmarJandali\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py in get_response
                                response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
C:\Users\OmarJandali\Desktop\opentab\opentab\tab\views.py in profileSetup
                            createUserSynapse(request) ...
▶ Local vars
C:\Users\OmarJandali\Desktop\opentab\opentab\tab\views.py in createUserSynapse
                call = call.json() ...
▶ Local vars
C:\Users\OmarJandali\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\models.py in json
                    return complexjson.loads(self.text, **kwargs) ...
▶ Local vars
C:\Users\OmarJandali\AppData\Local\Programs\Python\Python36\lib\site-packages\simplejson\__init__.py in loads
                    return _default_decoder.decode(s) ...
▶ Local vars
C:\Users\OmarJandali\AppData\Local\Programs\Python\Python36\lib\site-packages\simplejson\decoder.py in decode
                    obj, end = self.raw_decode(s) ...
▶ Local vars
C:\Users\OmarJandali\AppData\Local\Programs\Python\Python36\lib\site-packages\simplejson\decoder.py in raw_decode
                    return self.scan_once(s, idx=_w(s, idx).end()) ...
▶ Local vars
C:\Users\OmarJandali\AppData\Local\Programs\Python\Python36\lib\site-packages\simplejson\scanner.py in scan_once
                        return _scan_once(string, idx) ...
▶ Local vars
C:\Users\OmarJandali\AppData\Local\Programs\Python\Python36\lib\site-packages\simplejson\scanner.py in _scan_once
                        raise JSONDecodeError(errmsg, string, idx) ...
▶ Local vars

您的post请求返回“正在运行!”无法将其转换为json。所以line:call=call.json()引发JSONDecodeError错误

基本问题是,您对API的调用似乎没有返回具有有效JSON的响应,因此当您调用该方法时,
请求
无法解析它,就像Muktadiur所说的那样

为了诊断这一点,您需要弄清楚返回的是什么,也就是说,它是一个失败的响应还是一种您不希望从API中得到的格式(例如,
内容类型
输入错误可能导致API返回与您期望的不同)

在交互式python会话中运行请求代码,像在Django视图中那样ping API,并查看实际得到的结果,这可能是值得的。我建议
curl
更容易做到这一点,但我认为您处于一个双赢的环境中


编辑:实际上,最简单的诊断方法可能是将调用移到json的问题调用上方的
print(call.content)
。您可能还应该添加
打印(call.status\u code)
。这将很快向您显示1)您实际返回的内容,以及2)您返回的HTTP状态代码。这将有助于dx了解API调用的错误之处。

我不知道这是否是问题所在,但您的“常量类型”标题名称拼写错误。您在哪里回溯?现在还不清楚这个错误发生在代码中的什么地方。我将在postso中添加回溯,当我注释掉我得到的代码行时,
ValueError at/setup_profile/dictionary update sequence元素#0的长度为8;2是必需的请求方法:POST请求URL:http://127.0.0.1:8000/setup_profile/ Django版本:1.8.6异常类型:ValueError异常值:字典更新序列元素#0的长度为8;2是必需的异常位置:C:\Users\OmarJandali\AppData\Local\Programs\Python\36\lib\site packages\django\template\context.py在uuu init_uu,第20行
render-method-expecting-dictionary中,您提供的“call”不是dict。如何在交互式Python会话中运行请求。我用django在我的机器上运行了一个本地主机版本。我还可以去掉请求的json和头。。那会有什么用吗?…我在重新任务中去掉了头和有效载荷以及paload和头。。我收到了相同的错误消息。。。这是我能想到的唯一一个给我带来问题的地方,如果不是,那会是什么…用django发送json请求的最好方式是什么,让我传递标题和内容。。。Liek格式方面,因为我在网上查看了一下,但在Django中找不到发送和接收json请求的正确方法,所以我将修改我的评论,因为我看到了更多的代码。