English 中文(简体)
Handling POST, PUT, PATCH & DELETE Requests
  • 时间:2024-12-22

Handpng POST, PUT, PATCH and DELETE Requests


Previous Page Next Page  

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 =  https://postman-echo.com/post 
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":"https://postman-echo.com/post"}

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 =  https://postman-echo.com/put 
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":"https://postman-echo.com/put"}

Using PATCH

For the PATCH request, the Requests pbrary has requests.patch() method, the example of it is shown below.


import requests
myurl = https://postman-echo.com/patch 
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":"https://postman-echo.com/patch"}

Using DELETE

For the DELETE request, the Requests pbrary has requests.delete() method, the example of it is shown below.


import requests
myurl =  https://postman-echo.com/delete 
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":"https://postman-echo.com/delete"}
Advertisements