Reputation: 16479
I am a bit confused with using Request, urlopen and JSONDecoder().decode().
Currently I have:
hdr = {'User-agent' : 'anything'} # header, User-agent header describes my web browser
I am assuming that the server uses this to determine which browsers are acceptable? Not sure
my url is:
url = 'http://wwww.reddit.com/r/aww.json'
I set a req variable
req = Request(url,hdr) #request to access the url with header
json = urlopen(req).read() # read json page
I tried using urlopen in terminal and I get this error:
TypeError: must be string or buffer, not dict # This has to do with me header?
data = JSONDecoder().decode(json) # translate json data so I can parse through it with regular python functions?
I'm not really sure why I get the TypeError
Upvotes: 2
Views: 4673
Reputation: 388313
If you look at the documentation of Request
, you can see that the constructor signature is actually Request(url, data=None, headers={}, …)
. So the second parameter, the one after the URL, is the data you are sending with the request. But if you want to set the headers instead, you will have to specify the headers
parameter.
You can do this in two different ways. Either you pass None
as the data parameter:
Request(url, None, hdr)
But, well, this requires you to pass the data
parameter explicitely and you have to make sure that you pass the default value to not cause any unwanted effects. So instead, you can tell Python to explicitely pass the header
parameter instead, without specifying data
:
Request(url, headers=hdr)
Upvotes: 4