Python Requests is a tool that enables the discrete features of Python to make HTTP requests to any API in the world. The Python requests library is a tool that can be used to run the operations of web applications using code. There are over a billion API requests/day, so naturally, in most cases, we depend on the Python Requests library.
- Install Python.
- Install Pip.
- Install Request library.
- Check for your python version $ python3 –version >>Python 3.8.10
- Check for pip version $ pip –version >>pip 20.0.2
- Install request Library using following command: $ pip install requests
r = requests.get(url = URL, params = PARAMS)
r = requests.post(url = API_ENDPOINT, data = data)
r = requests.put(url = API_ENDPOINT, data = data)
r = request.delete(url = URL, params = PARAMS)
To send a POST request in Python, we can use requests.post() method. The request.post() method accepts multiple things like URL, data, Json, and arguments and sends a post request to a specified URL.
Generally, to send a post request we would require two things:
- URL for service endpoints.
- Object which has the properties needed to be sent.
Since passing data as a parameter to the post method is optional, we can conclude that it is very similar to the get method.
In the same way, we can also use the get request by using ‘requests.get’ method. Once the requests is made, we validate the response by either printing out if the data is a small unit. But if the data is in large quantity, we can verify by checking its status code, that is asserting and checking if the status code is 200 or not.
For update, we use put request that is ‘requests.put()’ method. In order to use this method we have to use full request body in order to make changes like “name”, “gender”,” email”, and “status”.
Here we are updating user/30320 which we created previously. Once the put request is sent the changes are made in desired Json body of data.
Deleting can be done easily by just passing the id we want to delete, here we check by deleting user by requests.delete() method. Thus, we can further vary with the respective status code which is 204.
- Easy to fetch data from given URL.
- Using Requests, we can harness response in various forms like text format, binary response, Json response, and also raw response.
- Timeout feature can also be easily added to URL.
- Scraping the data from websites is easy using requests library.
- Easy to handle cookies and sessions.
- Slow execution speed compared to other tools like axios, webdriver IO etc.
- Although Requests library is quite famous it is still not included in python and it is been proposed that in future requests be distributed with python by default.