2👍
The problem is in this line: parsed = json.loads(r)
You’re reciving the json response but insted of feeding json
elements to json.loads
you’re instead feeding it <Response [200]>
>>> r = requests.get('http://www.google.com')
>>> r
<Response [200]>
>>> type(r)
<class 'requests.models.Response'>
(Look closely at the error message. Expected string or buffer
Which means you’re providing it something that is NOT string or buffer(an object in this case))
This is the reason why str(r)
didn’t work. Because it just converted <Response 200>
to '<Response 200>'
which obviously is not json.
change this line to parsed = json.loads(r.text)
.
>>> type(r.text)
<type 'unicode'>
and then parsed['objects'][0]['ip']
should give you the IP address 🙂
You can find more about the requests module here
5👍
You are passing a requests object instead of a str
object to json.loads()
. You need to change
parsed = json.loads(r)
to
parsed = json.loads(r.text)
Also, parsed['objects']
is a list, you need to access its first element & then get the key ip
:
>>> print(parsed['objects'][0]['ip'])
- [Django]-Django Rest framework: passing request object around makes it lose POST data in newer version
- [Django]-How can I fix the XSS with Django + jQuery?