2017-09-04 172 views
1

我有一个项目,我正在努力,我需要和我试图发送一个json请求。我收到一个错误,我不知道它与我要发送的请求有什么关系。JSONDecodeError期望值:第1行第1列(char 0)

以下是错误:

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":"[email protected]", 
      } 
     ], 
     "phone_numbers":[ 
      "123.456.7890", 
      "[email protected]", 
     ], 
     "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 
+0

我不知道这是否是问题,但您的“常量型”标题名拼写错误。 –

+0

你追溯到哪里?目前还不清楚你的代码在哪里发生这个错误。 [mcve] –

+0

我将在后 –

回答

2

您的回覆请求返回n b'Running!'无法转换为json。所以行:call = call.json()引发JSONDecodeError错误。

+0

添加回溯,所以当我注释掉代码行时,我得到这个问题'/valueError at/setup_profile/ 字典更新序列元素#0的长度为8; 2需要 请求方法:\t POST 请求URL:\t http://127.0.0.1:8000/setup_profile/ Django的版本:\t 1.8.6 异常类型:\t ValueError异常 异常值:\t 词典更新序列元件#0长度为8; 2需要 例外位置:\t C:\ Users \ OmarJandali \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ django \ template \ context.py in __init__,line 20' –

+0

render方法期望dictionary和你提供的不是字典的“呼叫”。 – muktadiur

1

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

为了诊断这个问题,你需要弄清楚是什么得到returend,也就是说它是一个失败的响应或者你不期待从API中获得的格式(所以content-type错字可能导致API返回例如,与您期望的不同)。

在交互式python会话中运行请求代码,像在Django视图中一样Ping API,看看你实际回来的代码可能是值得的。我建议curl可以更轻松地做到这一点,但我看到你处于Win环境。

编辑:实际上,诊断此问题的最简单方法可能是让您将呼叫转移到print(call.content)之上,然后再调用json。你也应该添加print(call.status_code)。这应该很快显示出你1)你实际回来的和2)你回来的HTTP状态码。这应该有助于帮助DX调用API调用。

+0

如何在交互式Python会话中运行请求。我使用django在本机上运行本地主机版本。我也可以摆脱JSON和标头的请求..会做任何事情...... –

+0

我摆脱了标题和有效载荷以及paload和标头在resquest ..我得到相同的错误信息...这是唯一的地方,我可以认为这是给我的问题,如果不是这样,它可能是什么...... –

+0

什么是最好的方式发送Django的json请求,让我通过标题和内容... Liek格式明智,因为我在网上查找,并找不到正确的方式发送和接收Django中的json请求 –

相关问题