Xử lý các yêu cầu POST, PUT, PATCH và DELETE

Trong chương này, chúng ta sẽ hiểu cách sử dụng phương thức POST bằng cách sử dụng thư viện yêu cầu và cũng truyền các tham số cho URL.

Sử dụng POST

Đối với yêu cầu PUT, thư viện Yêu cầu có phương thức request.post (), ví dụ về nó được hiển thị bên dưới:

yêu cầu nhập khẩu

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

Đầu ra

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":"application/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"}

Trong ví dụ được hiển thị ở trên, bạn có thể chuyển dữ liệu biểu mẫu dưới dạng cặp khóa-giá trị vào tham số dữ liệu bên trong request.post (). Chúng ta cũng sẽ xem cách làm việc với PUT, PATCH và DELETE trong mô-đun yêu cầu.

Sử dụng PUT

Đối với yêu cầu PUT, thư viện Yêu cầu có phương thức request.put (), ví dụ về nó được hiển thị bên dưới.

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

Đầu ra

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

Sử dụng PATCH

Đối với yêu cầu PATCH, thư viện Yêu cầu có phương thức request.patch (), ví dụ về nó được hiển thị bên dưới.

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

Đầu ra

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

Sử dụng DELETE

Đối với yêu cầu DELETE, thư viện Yêu cầu có phương thức request.delete (), ví dụ về nó được hiển thị bên dưới.

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

Đầu ra

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