mohammad khan
mohammad khan

Reputation: 51

I get this TypeError: expected string or buffer while trying to get the response from the server

I am sending a post request with the below data:

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

In return I got the below response:

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

Upvotes: 0

Views: 1143

Answers (1)

Andersson
Andersson

Reputation: 52685

First time you use response content as loads argument (dataa = json.loads(response.content)) while second time you use response object as loads argument (dataa = json.loads(res)) So try to replace

dataa = json.loads(res)

with

dataa = json.loads(res.content)

Upvotes: 1

Related Questions