Obsługa żądań POST, PUT, PATCH i DELETE

W tym rozdziale zrozumiemy, jak używać metody POST przy użyciu biblioteki request, a także przekazywać parametry do adresu URL.

Korzystanie z POST

W przypadku żądania PUT biblioteka Requests ma metodę request.post (), jej przykład pokazano poniżej -

żądania importu

myurl = 'https://postman-echo.com/post'
myparams = {'name': 'ABC', 'email':'[email protected]'}
res = requests.post(myurl, data=myparams)
print(res.text)

Wynik

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

W powyższym przykładzie możesz przekazać dane formularza jako parę klucz-wartość do parametru danych wewnątrz request.post (). Zobaczymy również, jak pracować z modułem PUT, PATCH i DELETE w żądaniach.

Korzystanie z PUT

W przypadku żądania PUT biblioteka Requests ma metodę request.put (), jej przykład pokazano poniżej.

import requests
myurl = 'https://postman-echo.com/put'
myparams = {'name': 'ABC', 'email':'[email protected]'}
res = requests.put(myurl, data=myparams)
print(res.text)

Wynik

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

Korzystanie z PATCH

W przypadku żądania PATCH biblioteka Requests ma metodę request.patch (), której przykład pokazano poniżej.

import requests
myurl = https://postman-echo.com/patch'
res = requests.patch(myurl, data="testing patch")
print(res.text)

Wynik

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

Używanie DELETE

W przypadku żądania DELETE biblioteka Requests ma metodę request.delete (), której przykład pokazano poniżej.

import requests
myurl = 'https://postman-echo.com/delete'
res = requests.delete(myurl, data="testing delete")
print(res.text)

Wynik

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