___|  _ \   |  |    |   |_ _|\ \     / ____|
 |     |   |  |  |    |   |  |  \ \   /  __|
 |   | |   | ___ __|  ___ |  |   \ \ /   |
\____|\___/     _|   _|  _|___|   \_/   _____| 

 --- A GOPHER-LIKE INTERFACE FOR HIVE BLOCKCHAIN ---

GET and POST requests using Python's Requests library

BY: @rohancmr | CREATED: Jan. 13, 2018, 5:51 p.m. | VOTES: 7 | PAYOUT: $38.46 | [ VOTE ]

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1515865604/bur2ylwpr97cjtoojhsi.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.

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

Features of Requests :

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

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1515863619/ysun3dqsldxjqwtepddl.gif]

Check if the library can be imported.

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1515863668/tcuadifyjdnz6agipf1l.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 requests

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

r = requests.get(url)

r.status_code

r.headers

r.text

r.json()

Full Code

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1515864138/dlcg875fxsx0qrq9elbc.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

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1515864182/ypw9sixkgv2bs8rrwnrj.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'}

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

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

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

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

Full code

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1515864626/s8gzgyb82ubsp5cd7935.png]

Output of above code

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1515864576/vakmhgs4zojswgi6l5dt.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'}

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

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

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1515864754/eatuqhmbtajzlovg9t5h.png]

Output of above code

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1515864769/demqtaerkpjwaruvy9ul.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

TAGS: [ #utopian-io ] [ #python ] [ #programming ] [ #requests ] [ #http ]

Replies

@shreyasgune | Jan. 14, 2018, 4:48 a.m. | Votes: 1 | [ VOTE ]

Thank you for the contribution. It has been approved.

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

@amrit1711 | Jan. 14, 2018, 5:12 a.m. | Votes: 0 | [ VOTE ]

Nice post

@rohancmr | Jan. 14, 2018, 11:16 a.m. | Votes: 0 | [ VOTE ]

@originalworks

@originalworks | Jan. 14, 2018, 12:11 p.m. | Votes: 0 | [ VOTE ]

The @OriginalWorks bot has determined this post by @rohancmr to be original material and upvoted it!
[IMAGE: https://steemitimages.com/DQmaBi37A5oTnQ9NBLH8YU4jpvhhmFauyvgg3YRrEJwskM9/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.

@utopian-io | Jan. 14, 2018, 6:34 p.m. | Votes: 0 | [ VOTE ]

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!
- Vote for my Witness With SteemConnect
- Proxy vote to Utopian Witness with SteemConnect
- Or vote/proxy on Steemit Witnesses

[IMAGE: https://steemitimages.com/DQmYPUuQRptAqNBCQRwQjKWAqWU3zJkL3RXVUtEKVury8up/mooncryption-s-utopian-io-witness-gif.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

[ BACK TO TRENDING ] [ BACK TO MENU ]
CMD>