Krishna
Krishna

Reputation: 1382

Python TypeError: expected string or buffer. Need the response in dictionary

I have the following code

import requests
import sys
import json

arg_count = 0

#check that correct number of arguments are passed to utility
for arg in sys.argv:
    arg_count = arg_count + 1

assert arg_count == 4

#parse the input arguments to the utility
connectplus_group_title = sys.argv[1]
connectplus_uname = sys.argv[2]
connectplus_passwords = sys.argv[3]

#find the group id from the input group name i.e connectplus_group_title
response = requests.get("https://connectplus.geometricglobal.com/_vti_bin/beezy/v2/api.svc//Groups/Availability?title={title})", auth=(connectplus_uname, connectplus_passwords))
#assert response.status_code == response.codes.ok
group_data = json.loads(response)
group_id = group_data[connectplus_group_title]

connectplus_group_contributors = "https://connectplus.geometricglobal.com/_vti_bin/beezy/v2/api.svc//Groups/{id}/Contributors?skip={skip}&take={take}"
response = requests.get(connectplus_group_contributors,auth=(connectplus_uname, connectplus_passwords))
data = response.json()

I need to get the json data which is returned by the URL , I have tried using json.loads(response.text) or simple json.loads(response) with no luck I keep getting the above error.

What is the correct way to get the data from a REST url in the from of dictionary.

Upvotes: 2

Views: 4045

Answers (1)

daveoncode
daveoncode

Reputation: 19618

You have to pass response.content, response is a Response type not a simple string (try type(response)) and json.loads needs (as the error highlights) a string or buffer.

Replace your call with:

json.loads(response.content)

Upvotes: 4

Related Questions