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
Check if the library can be imported.
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 inurl
variable).
url = "http://httpbin.org/get"
- To fetch the content of the page, pass the
url
variable to theget()
function in requests library and store the result in a variable. (I have store the result in a variabler
)
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
In above code I have used time.sleep(2)
to wait for 2 sec before each result is displayed.
Output of the above code
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 theget
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 ofargs
variable.
print("Json Output: {}".format(r.json()['args']))
Full code
Output of above code
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
Output of above code
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
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Nice post
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
@originalworks
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
The @OriginalWorks bot has determined this post by @rohancmr to be original material and upvoted it!
To call @OriginalWorks, simply reply to any post with @originalworks or !originalworks in your message!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hey @rohancmr I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
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
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit