0

in this test code sample:

import httplib2
import json
url="https://www.googleapis.com/oauth2/v1/tokeninfo? 
     access_token=61651651651"
h=httplib2.Http()
test1=json.loads(h.request(url,'GET')[1])

i don't understand what is the meaning of [1] in the last line, if i use [2] for example i get an error IndexError: tuple index out of range, [0] gives me TypeError: the JSON object must be str, bytes or bytearray, not 'Response', so every time i load json response i have to use [1]?

  • Since you asked, that's your answer. Though I suspect your actual issue may be uncovered once you decide to share more than one line of code. – cs95 Jan 14 '18 at 04:54
  • Seems like you are passing object of type `Response` to `json.loads`. It only accepts `bytes` or `str`. – Sohaib Farooqi Jan 14 '18 at 04:56
  • @cᴏʟᴅsᴘᴇᴇᴅ Bad dupe, this isn't slicing – L3viathan Apr 27 '18 at 21:02
  • @L3viathan, ...though the answer there *does* also describe indexing into a list as-such as well. – Charles Duffy Apr 27 '18 at 21:04
  • 1
    @SergioAlvarezPinto I'd suggest logging `repr(h.request(url,'GET'))` somewhere you can inspect it. You'll see that it has two parts, and that they materially differ from each other (one being head, the other being body content) in a way that makes the necessity to use the 2nd one to parse as JSON (when the returned body is in fact JSON) obvious. – Charles Duffy Apr 27 '18 at 21:05
  • 1
    (This is much more a question about `httplib2`, btw; it has very little to do with `json.loads()`). – Charles Duffy Apr 27 '18 at 21:17

0 Answers0