2017-09-04 177 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請求 –

相關問題