Link to home
Start Free TrialLog in
Avatar of forums_mp
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-type')
'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 ?
Avatar of pepr
pepr

You may have overlooked or you were thinking about something else, but the Response object does have the get() method:
requests.get(url, **kwargs)

    Sends a GET request. Returns Response object.
    Parameters:      

        url – URL for the new Request object.
        **kwargs – Optional arguments that request takes.

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.
Avatar of forums_mp

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

Open in new window

?
ASKER CERTIFIED SOLUTION
Avatar of pepr
pepr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial