?
Solved

python requests API

Posted on 2014-07-24
3
Medium Priority
?
281 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 200 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
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 while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses

762 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