Table of contents
1.
Introduction
2.
Advanced Methods in requests
2.1.
Post Method
2.1.1.
Making the POST request through Python Requests
2.2.
Put Method
2.2.1.
Making the PUT request through Python Requests
2.3.
Delete Method
2.3.1.
Making the DELETE request through Python Requests
2.4.
Patch Method
2.4.1.
Making the PATCH request through Python Requests
2.5.
Head Method
2.5.1.
Making the HEAD request through Python Requests
3.
FAQs
4.
Key Takeaways
Last Updated: Mar 27, 2024

Advanced Methods in requests

Author Sanjana Yadav
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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)
You can also try this code with Online Python Compiler
Run Code

Example:

import requests
param = { 'custname':'abcd', 'custemail': 'efgh@gmail.com'}
r = requests.post('http://httpbin.org/post', data=param)
print(r)
print(r.json())
You can also try this code with Online Python Compiler
Run Code

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)
You can also try this code with Online Python Compiler
Run Code

Example:

import requests 
r = requests.put('https://httpbin.org/put', data ={'name':'abcd'})
print(r) 
print(r.content)
You can also try this code with Online Python Compiler
Run Code

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)
You can also try this code with Online Python Compiler
Run Code

Example:

import requests 
r = requests.delete('https://httpbin.org/delete', data ={'name':'abcd'}) 
print(r) 
print(r.json())
You can also try this code with Online Python Compiler
Run Code

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)
You can also try this code with Online Python Compiler
Run Code

Example:

import requests 
r = requests.patch('https://httpbin.org/patch', data ={'name':'abcd'}) 
print(r) 
print(r.json())
You can also try this code with Online Python Compiler
Run Code

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)
You can also try this code with Online Python Compiler
Run Code

Example:

import requests 
r = requests.head('https://httpbin.org/', data ={'key':'value'}) 
print(r) 
print(r.headers) 
print(r.text)
You can also try this code with Online Python Compiler
Run Code

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'}
> 

FAQs

  1. What are HTTP request methods?
    POST, GET, PUT, PATCH, and DELETE are the most regularly used HTTP methods. These methods relate to the CRUD (create, read, update, and delete) operations. There are a few more options as well, although they're used less commonly. 
     
  2. How do you pass parameters in Python requests?
    To transmit parameters through URL, create a dictionary with all parameter key: value pairs and pass it as a params argument to any GET, POST, PUT, HEAD, DELETE, or OPTIONS request. The final link would be https://somewebsite.com/?param1=value1&param2=value2.
     
  3. Is requests part of the Python standard library?
    Requests is a Python library that enables you to use basic Python dictionaries to submit HTTP/1.1 requests, add headers, form data, multipart files, and arguments. In the same way, it gives you access to the answer data.

Key Takeaways

  • Cheers if you reached here! In this blog, we learned several advanced methods present in the requests library in python. 
  • With the help of examples of each, we saw the post, put, delete, patch, and head methods.

On the other hand, learning never ceases, and there is always more to learn. So, keep learning and keep growing, ninjas!
Check out the Python Interview Questions to get hands-on experience with frequently asked interview questions and land your dream job.
Good luck with your preparation!

Live masterclass