GET and POST requests using Python's Requests library

in utopian-io •  7 years ago  (edited)

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

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Nice post

The @OriginalWorks bot has determined this post by @rohancmr to be original material and upvoted it!

ezgif.com-resize.gif

To call @OriginalWorks, simply reply to any post with @originalworks or !originalworks in your message!

Please note that this is a BETA version. Feel free to leave a reply if you feel this is an error to help improve accuracy.

Hey @rohancmr I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • This is your first accepted contribution here in Utopian. Welcome!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x