2016-08-02 50 views
0

我送與下面的數據POST請求的響應:我得到這個類型錯誤:預期的字符串或緩衝區,而試圖從服務器獲取

data = '{"email":"[email protected]","password":"abcdef"}' 

response = requests.post(login, data=data, headers={"Content-Type":"application/json"}) 

dataa = json.loads(response.content) 

print dataa 

token = dataa['token'] 

print "Access_token", token 

headers = {"Content-Type":"application/json","Authorization":token} 

data ='{"form":{"projectLocations":"LOPP","status":"Active","active":true,"name":"AWS","contactName":"xyz", 
"contactEmail":"[email protected]","customId":"102","createdCompany":"57972dcad9562e661c73707"}}' 

res = requests.post(create_project, data=data, headers={"Content-Type":"application/json"}) 

dataa = json.loads(res) 

print res 

作爲回報,我得到了如下回應:

dataa = json.loads(res) 

File "C:\Python27\lib\json\__init__.py", line 338, in loads 

return _default_decoder.decode(s) 

File "C:\Python27\lib\json\decoder.py", line 366, in decode 

obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 

TypeError: expected string or buffer 
+0

你明白了什麼,當你做'打印res.text'? – DeepSpace

+0

同樣的錯誤,當我打印res.text。 –

+0

請務必在'dataa = json.loads(res)'上方放置'print res.text' **,否則您將看到相同的異常。 – DeepSpace

回答

1

第一次使用響應內容爲loads參數(dataa = json.loads(response.content)),而第二次使用響應對象作爲參數loadsdataa = json.loads(res)) 所以試圖取代

dataa = json.loads(res) 

dataa = json.loads(res.content) 
+0

謝謝,我犯了一個愚蠢的錯誤。 :) –

相關問題