rohan avatar

GET and POST requests using Python's Requests library

rohancmr

Published: 13 Jan 2018 › Updated: 13 Jan 2018GET and POST requests using Python's Requests library

GET and POST requests using Python's Requests library

requests-sidebar.png

Requests is a Python library, used for all kinds of HTTP requests. It is developed by Kenneth Reitz and released under Apache2 license. The goal of this project is to make http requests simpler and more human friendly.

Request was developed with below PEP 20 idioms in mind.

  • Beautiful is better than ugly.
  • Explicit is better than implicit.
  • Simple is better than complex.
  • Complex is better than complicated.
  • Readability counts.

Requests is one of the most downloaded Python packages of all time.

Features of Requests :

  • Basic/Digest Authentication
  • Sessions with Cookie Persistence
  • Browser-style SSL Verification
  • Supports the entire restful API, i.e., all its methods – PUT, GET, DELETE, POST
  • Has a built-in JSON decoder
  • Multipart File Uploads

Installation :

Simplest method to install requests is by using pip. In Windows, pip is available under "script" directory and in linux under "bin" directory of Python's installation path.

Installing requests using pip

pip install requests

install.gif

Check if the library can be imported.

test_install.gif

Let us now see how we can use requests for GET and POST calls.

Get Requests :

Example 1 : Get the content of http://httpbin.org/get page using requests library.

  • Import the requests module to make http GET request .

import requests

  • store the URL http://httpbin.org/get in a variable. (In below example, I have stored the URL in url variable).

url = "http://httpbin.org/get"

  • To fetch the content of the page, pass the url variable to the get() function in requests library and store the result in a variable. (I have store the result in a variable r)

r = requests.get(url)

  • Use status_code to fetch the http return code. If the request was successful, the return code is 200 .

r.status_code

  • Use headers to fetch header of the request.

r.headers

  • Use text to fetch the output in text format.

r.text

  • Use json() to fetch the output in json format.

r.json()

Full Code

simple_get_request.PNG

In above code I have used time.sleep(2) to wait for 2 sec before each result is displayed.

Output of the above code

simple_get_request.gif

Example 2 : Passing Parameters to the GET function : Pass below dictionary to http://httpbin.org/get url and display the passed parameters in the output.

Dictionary is {'language': 'python', 'library': 'requests'}

  • Store the above dictionary in a variable. In this example we have stored it in payload variable.

payload = {'language': 'python', 'library': 'requests'}

  • Use params, in the get function to pass the above dictionary as a parameter.

r = requests.get(url, params=payload)

  • To check the complete URL with parameter use url.

print("URL: {} \n".format(r.url))

  • In the result, the passed parameter is stored in args. To fetch it value, convert the output of request to json and fetch the value of args variable.

print("Json Output: {}".format(r.json()['args']))

Full code

get_with_parameters.PNG

Output of above code

get_with_parameters.gif

Note: Since this is a GET request, the passed parameters are visible in URL.

POST Request

Example 3: Make a POST request to http://httpbin.org/post with below dictionary as parameter and display the result in a text format.

Dictionary is {'language': 'python', 'library': 'requests'}

  • Store the above dictionary in a variable. (I have stored it in values variable).

values = {'language': 'python', 'library': 'requests'}

  • Send a http POST request to the URL with the parameters, using the post() function.

r = requests.post('http://httpbin.org/post', data=values)

Similar to example 1, we have use time library to delay the output by 2 sec.

Full Code

post_request.PNG

Output of above code

post_example.gif

Note: Since this is a POST request, the passed parameter is not visible in the URL.

Conclusion

The python's requests library is a very handy tool when trying to scrape some webpages for information or developing tools using rest API. We can also download music files, wallpapers, etc from different websites once we have the URL.

If you have any questions, comments or have used Requests before, I'd would love to hear from you in comment section.



Posted on Utopian.io - Rewarding Open Source Contributors

Leave GET and POST requests using Python's Requests library to:

Written by

Read more #utopian-io posts


Best Posts From rohan

We have not curated any of rohancmr's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From rohan