Solved

python requests API

Posted on 2014-07-24
3
218 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
  • 2
3 Comments
 
LVL 28

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 28

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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 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 …

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now