Gestion des requêtes POST, PUT, PATCH et DELETE
Dans ce chapitre, nous allons comprendre comment utiliser la méthode POST à l'aide de la bibliothèque de requêtes et également transmettre des paramètres à l'URL.
Utiliser POST
Pour la requête PUT, la bibliothèque Requests a la méthode requests.post (), l'exemple en est montré ci-dessous -
demandes d'importation
myurl = 'https://postman-echo.com/post'
myparams = {'name': 'ABC', 'email':'[email protected]'}
res = requests.post(myurl, data=myparams)
print(res.text)
Production
E:\prequests>python makeRequest.py
{"args":{},"data":"","files":{},"form":{"name":"ABC","email":"[email protected]"},
"headers":{"x-forwarded-proto":"https","host":"postman-echo.com","content-
length":"30","accept":"*/*","accept-encoding":"gzip,deflate","content-
type":"application/x-www-form-urlencoded","user-agent":"python-
requests/2.22.0","x-forwarded-
port":"443"},"json":{"name":"ABC","email":"[email protected]"},
"url":"https://postman-echo.com/post"}
Dans l'exemple ci-dessus, vous pouvez transmettre les données du formulaire en tant que paire clé-valeur au paramètre de données dans requests.post (). Nous verrons également comment travailler avec PUT, PATCH et DELETE dans le module de requêtes.
Utilisation de PUT
Pour la requête PUT, la bibliothèque Requests a la méthode requests.put (), l'exemple en est montré ci-dessous.
import requests
myurl = 'https://postman-echo.com/put'
myparams = {'name': 'ABC', 'email':'[email protected]'}
res = requests.put(myurl, data=myparams)
print(res.text)
Production
E:\prequests>python makeRequest.py
{"args":{},"data":"","files":{},"form":{"name":"ABC","email":"[email protected]"},
"headers":{"x-forwarded-proto":"https","host":"postman-echo.com","content-
length":
"30","accept":"*/*","accept-encoding":"gzip, deflate","content-
type":"applicatio
n/x-www-form-urlencoded","user-agent":"python-requests/2.22.0","x-forwarded-
port
":"443"},"json":{"name":"ABC","email":"[email protected]"},
"url":"https://postman-echo.com/put"}
Utilisation de PATCH
Pour la requête PATCH, la bibliothèque Requests a la méthode requests.patch (), l'exemple de celle-ci est montré ci-dessous.
import requests
myurl = https://postman-echo.com/patch'
res = requests.patch(myurl, data="testing patch")
print(res.text)
Production
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"}
Utiliser DELETE
Pour la requête DELETE, la bibliothèque Requests dispose de la méthode requests.delete (), dont l'exemple est illustré ci-dessous.
import requests
myurl = 'https://postman-echo.com/delete'
res = requests.delete(myurl, data="testing delete")
print(res.text)
Production
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"}