Requests Tutorial
Selected Reading
- Requests - Discussion
- Requests - Useful Resources
- Requests - Quick Guide
- Requests - Web Scraping using Requests
- Requests - Proxy
- Requests - Event Hooks
- Requests - Authentication
- Requests - SSL Certification
- Requests - Handling Sessions
- Requests - Handling History
- Requests - Handling Redirection
- Requests - Handling Timeouts
- Requests - Working with Errors
- Requests - Working with Cookies
- Requests - File Upload
- Handling POST, PUT, PATCH & DELETE Requests
- Requests - Handling GET Requests
- Requests - HTTP Requests Headers
- Handling Response for HTTP Requests
- Requests - Working with Requests
- Requests - How Http Requests Work?
- Requests - Environment Setup
- Requests - Overview
- Requests - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Handling POST, PUT, PATCH & DELETE Requests
Handpng POST, PUT, PATCH and DELETE Requests
In this chapter, we will understand how to use the POST method using requests pbrary and also pass parameters to the URL.
Using POST
For PUT request, the Requests pbrary has requests.post() method, the example of it is shown below −
import requests
myurl =myparams = { name : ABC , email : xyz@gmail.com } res = requests.post(myurl, data=myparams) print(res.text)
Output
E:prequests>python makeRequest.py {"args":{},"data":"","files":{},"form":{"name":"ABC","email":"xyz@gmail.com"}, "headers":{"x-forwarded-proto":"https","host":"postman-echo.com","content- length":"30","accept":"*/*","accept-encoding":"gzip,deflate","content- type":"apppcation/x-www-form-urlencoded","user-agent":"python- requests/2.22.0","x-forwarded- port":"443"},"json":{"name":"ABC","email":"xyz@gmail.com"}, "url":""}
In the example shown above, you can pass the form data as key-value pair to the data param inside requests.post(). We will also see how to work with PUT, PATCH and DELETE in requests module.
Using PUT
For PUT request, the Requests pbrary has requests.put() method, the example of it is shown below.
import requests myurl =myparams = { name : ABC , email : xyz@gmail.com } res = requests.put(myurl, data=myparams) print(res.text)
Output
E:prequests>python makeRequest.py {"args":{},"data":"","files":{},"form":{"name":"ABC","email":"xyz@gmail.com"}, "headers":{"x-forwarded-proto":"https","host":"postman-echo.com","content- length": "30","accept":"*/*","accept-encoding":"gzip, deflate","content- type":"apppcatio n/x-www-form-urlencoded","user-agent":"python-requests/2.22.0","x-forwarded- port ":"443"},"json":{"name":"ABC","email":"xyz@gmail.com"}, "url":""}
Using PATCH
For the PATCH request, the Requests pbrary has requests.patch() method, the example of it is shown below.
import requests myurl =res = requests.patch(myurl, data="testing patch") print(res.text)
Output
E:prequests>python makeRequest.py {"args":{},"data":{},"files":{},"form":{},"headers":{"x-forwarded- proto":"https" ,"host":"postman-echo.com","content-length":"13","accept":"*/*","accept- encoding ":"gzip, deflate","user-agent":"python-requests/2.22.0","x-forwarded- port":"443" },"json":null,"url":""}
Using DELETE
For the DELETE request, the Requests pbrary has requests.delete() method, the example of it is shown below.
import requests myurl =res = requests.delete(myurl, data="testing delete") print(res.text)
Output
E:prequests>python makeRequest.py {"args":{},"data":{},"files":{},"form":{},"headers":{"x-forwarded- proto":"https" ,"host":"postman-echo.com","content-length":"14","accept":"*/*","accept- encoding ":"gzip, deflate","user-agent":"python-requests/2.22.0","x-forwarded- port":"443" },"json":null,"url":""} Advertisements