What is the Python requests module?
The requests module in Python is used to make HTTP calls – it has a simple, elegant API.
Most commonly used HTTP methods
The four most commonly used methods are:
get()
The get() method is used to make HTTP get requests.
The get method signature is:
requests.get(url, params=None, **kwargs)
| param | description |
|---|---|
| url | Http url |
| params | Dictionary, list of tuples or bytes to send in the query string |
| kwargs | Optional arguments that Request takes |
import requests
url = "https://httpbin.org"
response = requests.get(url)
print(response.text)
post()
The post() method is used to make HTTP post requests.
The post method signature is:
requests.post(url, data=None, json=None, **kwargs)
| param | description |
|---|---|
| url | Http Url |
| data | Dictionary, list of tuples, bytes, or file-like object to send in the body of the request |
| json | json data to send in the body of the Request |
| kwargs | Optional arguments that Request takes |
import requests
url = 'https://httpbin.org/post'
data = {"website": "educative.io"}
response = requests.post(url, data=data)
print(response.json())
put()
The put() method is used to make HTTP put requests.
The put method signature is:
requests.put(url, data=None, **kwargs)
| param | description |
|---|---|
| url | Http Url |
| data | Dictionary, list of tuples, bytes, or file-like object to send in the body of the request |
| kwargs | Optional arguments that Request takes |
import requests
url = 'https://httpbin.org/put'
data = {"website": "educative.io"}
response = requests.put(url, data=data)
print(response.json())
delete()
The delete() method is used to make HTTP delete requests.
The delete method signature is:
requests.delete(url, **kwargs)
| param | description |
|---|---|
| url | Http Url |
| kwargs | Optional arguments that Request takes |
import requests
url = 'https://httpbin.org/delete'
response = requests.delete(url)
print(response.json())
Use response.status_code for http status code sent by the host server.
Tips
- Use the
raise_for_status()method on the response object to raise an exception for certain response codes. - Use
requests.session()- If you are making several requests to the same host as the underlying tcp connection will be reused.
- If certain parameters are to be shared across requests, like cookies.
How to install requests
The easiest way to install requests is through pip:
pip install requests
Using Proxy in Requests
When making an HTTP call, you can use proxies to prevent your online identity, i.e., IP address.
The format for a proxy is:
http(s)://username:password@ip_addrs:port
- Define a dictionary for
http,https, andsocksproxy. - Specify the above dictionary when making the
requestscall.
import requests
proxy_dict = {'http': 'http://201.220.140.30:8181', 'https': 'https://201.220.140.30:8181'}
url = "https://httpbin.org"
response = requests.get(url, proxies=proxy_dict)
print(response.text)
If the proxies are not working, then the ProxyError exception will be thrown.
SSL Verification
The requests module verifies SSL certificates for the secure URLS, i.e., HTTPs URLs. By default, requests verifies the SSL certificates. You can disable the verification by using verify=False while making the request.
response = requests.get(url, proxies=proxy_dict, verify=False)
You can also specify a client side SSL certificate while making the request.
Pass the file path of the cert file to the verify parameter:
response = requests.get(url, verify='/path/to/certfile')
A SSLError