Solved

python requests API

Posted on 2014-07-24
3
252 Views
Last Modified: 2014-08-13
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 ?
0
Comment
Question by:forums_mp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 29

Expert Comment

by:pepr
ID: 40218643
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.
0
 

Author Comment

by:forums_mp
ID: 40218824
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

?
0
 
LVL 29

Accepted Solution

by:
pepr earned 50 total points
ID: 40219015
It makes sense to make r.headers a dictionary. Then r.headers.get('content-type') is exactly the same as r.headers['content-type']. The .get() of a dictionary is completely unrelated to the http GET, POST, DELETE... The r.headers is a data structure with key/value pairs.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question