Manejo de solicitudes POST, PUT, PATCH y DELETE

En este capítulo, entenderemos cómo usar el método POST usando la biblioteca de solicitudes y también pasar parámetros a la URL.

Usando POST

Para la solicitud PUT, la biblioteca de solicitudes tiene el método request.post (), el ejemplo se muestra a continuación:

solicitudes de importación

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

Salida

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

En el ejemplo que se muestra arriba, puede pasar los datos del formulario como un par clave-valor al parámetro de datos dentro de las solicitudes.post (). También veremos cómo trabajar con PUT, PATCH y DELETE en el módulo de solicitudes.

Usando PUT

Para la solicitud PUT, la biblioteca Requests tiene el método request.put (), el ejemplo se muestra a continuación.

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

Salida

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

Usando PATCH

Para la solicitud PATCH, la biblioteca Requests tiene el método request.patch (), el ejemplo se muestra a continuación.

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

Salida

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

Usando ELIMINAR

Para la solicitud DELETE, la biblioteca Requests tiene el método request.delete (), el ejemplo se muestra a continuación.

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

Salida

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