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
requestsmodule to make http GET request .
import requests
- store the URL
http://httpbin.org/getin a variable. (In below example, I have stored the URL inurlvariable).
url = "http://httpbin.org/get"
- To fetch the content of the page, pass the
urlvariable 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_codeto fetch the http return code. If the request was successful, the return code is 200 .
r.status_code
- Use
headersto fetch header of the request.
r.headers
- Use
textto 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
payloadvariable.
payload = {'language': 'python', 'library': 'requests'}
- Use
params, in thegetfunction 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 ofargsvariable.
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
valuesvariable).
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