我得到这个TypeError:在尝试从服务器获取响应时期望的字符串或缓冲区

时间:2016-08-02 07:46:05

标签: json python-2.7 python-requests urllib2

我发送的帖子请求包含以下数据:

data = '{"email":"abc@gmail.com","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":"stu@gmail.com","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

1 个答案:

答案 0 :(得分:1)

第一次将响应内容用作loads参数(dataa = json.loads(response.content)),而第二次将响应对象用作loads参数(dataa = json.loads(res)) 所以尝试替换

dataa = json.loads(res)

dataa = json.loads(res.content)