Introduction
The Requests package is an essential feature of Python that allows you to make HTTP requests to a particular URL. Requests must be learned before moving further with these technologies, whether REST APIs or Web Scraping. A response is returned when a request is made to a URL. Python requests has built-in management features for both the request and the response.
The Python requests module has various built-in methods for making HTTP requests to a given URI using the GET, POST, PUT, PATCH, or HEAD protocols. An HTTP request is used to get data from a certain URI or to send data to a server. It functions as a client-server request-response protocol.
Before moving forward with this article you must be familiar with the basic methods and the features of the requests module.
Advanced Methods in requests
Post Method
POST is an HTTP request method supported by the World Wide Web. The POST request method is designed to request that a web server accept the data included in the body of the request message, most likely for storage. It is frequently utilized when uploading a file or submitting a completed online form.
Making the POST request through Python Requests
For performing a POST request to a specific URI, Python's requests module has a built-in function named post().
Syntax:
requests.post(url, params={key: value}, args)Example:
import requests
param = { 'custname':'abcd', 'custemail': 'efgh@gmail.com'}
r = requests.post('http://httpbin.org/post', data=param)
print(r)
print(r.json())Output:
<Response [200]>
{'args': {}, 'data': '', 'files': {}, 'form': {'custemail': 'efgh@gmail.com', 'custname': 'abcd'}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '40', 'Content-Type': 'application/x-www-form-urlencoded', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.22.0', 'X-Amzn-Trace-Id': 'Root=1-621a7845-69c31e6656d6ee3277f32053'}, 'json': None, 'origin': '34.67.1.198', 'url': 'http://httpbin.org/post'}
>
Put Method
PUT is an HTTP request method that is supported by the World Wide Web. The PUT method asks for the contained entity to be saved at the specified URI. If the URI relates to an existing resource, it is updated; if it does not, the server can build the resource with that URI.
Making the PUT request through Python Requests
The put() function in Python's requests module is used to make a PUT request to a given URI.
Syntax:
requests.put(url, params={key: value}, args)Example:
import requests
r = requests.put('https://httpbin.org/put', data ={'name':'abcd'})
print(r)
print(r.content)Output:
<Response [200]>
b'{\n "args": {}, \n "data": "", \n "files": {}, \n "form": {\n "name": "abcd"\n }, \n "headers": {\n "Accept": "*/*", \n "Accept-Encoding": "gzip, deflate", \n "Content-Length": "9", \n "Content-Type": "application/x-www-form-urlencoded", \n "Host": "httpbin.org", \n "User-Agent": "python-requests/2.22.0", \n "X-Amzn-Trace-Id": "Root=1-621a7402-6c644aa7237e1314423cfa8a"\n }, \n "json": null, \n "origin": "34.135.46.166", \n "url": "https://httpbin.org/put"\n}\n'
>
Delete Method
DELETE is an HTTP request method provided by the World Wide Web. The DELETE method removes the provided resource from the system. Like a PUT request, this procedure requires you to identify a specific resource.
If the answer includes an entity describing the status, the response code is 200 (OK).
200(Accepted)If the action has not yet been implemented.
If the operation has been completed, but the answer does not include an entity, the response code is 204 (No Content).
Making the DELETE request through Python Requests
The delete() function in Python's requests module is used to make a DELETE request to a given URI.
Syntax:
requests.delete(url, params={key: value}, args)Example:
import requests
r = requests.delete('https://httpbin.org/delete', data ={'name':'abcd'})
print(r)
print(r.json())Output:
<Response [200]>
{'args': {}, 'data': '', 'files': {}, 'form': {'name': 'abcd'}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '9', 'Content-Type': 'application/x-www-form-urlencoded', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.22.0', 'X-Amzn-Trace-Id': 'Root=1-621a83a5-45a6017a1e6201b302d08035'}, 'json': None, 'origin': '34.135.46.166', 'url': 'https://httpbin.org/delete'}
>
Practice this code with the help of Online Python Compiler
Patch Method
PATCH is an HTTP request mechanism used by the World Wide Web. It is used to change capabilities. The PATCH request must only provide the modifications to the resource, not the complete resource. This is similar to PUT, but the body provides a series of instructions detailing how to modify a resource presently on the server to generate a new version. This means that the PATCH body should not just be a changed portion of the resource, but should be written in a patch language such as JSON Patch or XML Patch. PATCH is neither safe nor irreversible.
Making the PATCH request through Python Requests
Patch() is a built-in function in Python's requests module for making a PATCH request to a given URI.
Syntax:
requests.patch(url, params={key: value}, args)Example:
import requests
r = requests.patch('https://httpbin.org/patch', data ={'name':'abcd'})
print(r)
print(r.json())Output:
<Response [200]>
{'args': {}, 'data': '', 'files': {}, 'form': {'name': 'abcd'}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '9', 'Content-Type': 'application/x-www-form-urlencoded', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.22.0', 'X-Amzn-Trace-Id': 'Root=1-621a85c7-69010d707e15c31a7ede8f77'}, 'json': None, 'origin': '34.67.1.198', 'url': 'https://httpbin.org/patch'}
>
Head Method
HEAD is an HTTP request mechanism used by the World Wide Web. The HEAD method requests a response equivalent to a GET request but without the response body. This is handy for getting meta-data from response headers without having to transfer the complete content.
Making the HEAD request through Python Requests
The head() function in Python's requests module is used to make a HEAD request to a given URI.
Syntax:
requests.patch(url, params={key: value}, args)Example:
import requests
r = requests.head('https://httpbin.org/', data ={'key':'value'})
print(r)
print(r.headers)
print(r.text)Output:
<Response [200]>
{'Date': 'Sat, 26 Feb 2022 20:01:31 GMT', 'Content-Type': 'text/html; charset=utf-8', 'Content-Length': '9593', 'Connection': 'keep-alive', 'Server': 'gunicorn/19.9.0', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true'}
>




