Solved

python requests API

Posted on 2014-07-24
3
243 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
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 lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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…

749 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