forums_mp
asked on
python requests API
Playing around with python and I'm confused on the API. Given:
http://docs.python-requests.org/en/latest/api/, specifically: class requests.Response and the 'headers' attribute.
Now reference:
http://docs.python-requests.org/en/latest/user/quickstart/, specifically:
>>> r.headers['Content-Type']
'application/json'
>>> r.headers.get('content-typ e')
'application/json'
I understand r.headers[], however, I'm not understanding r.headers.get(''). How do they arrive at a get method when the Response object (first link) does not have a get method ?
http://docs.python-requests.org/en/latest/api/, specifically: class requests.Response and the 'headers' attribute.
Now reference:
http://docs.python-requests.org/en/latest/user/quickstart/, specifically:
>>> r.headers['Content-Type']
'application/json'
>>> r.headers.get('content-typ
'application/json'
I understand r.headers[], however, I'm not understanding r.headers.get(''). How do they arrive at a get method when the Response object (first link) does not have a get method ?
ASKER
I think I'm following you. I'm unable to try this right now given I'm not in front of a compiler, however I'm assuming I should be able to access the other methods in requests via headers from the response object. I.e
r.headers.put
r.headers.delete
etc
?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You were apparently thinking about the headers which is a dictionary. The (standard) dictionary type has the .get() method which does what basically [] does. However, the .get() method may have optional second argument that says what is the default returned if the key is not in the dictionary.