The urllib3 library provides a simple way to make HTTP requests in Python. This includes sending POST requests to submit data to APIs and web services.
To make a POST request with urllib3, first install the library:
pip install urllib3
Then import
import urllib3
http = urllib3.PoolManager()
Construct the data you want to submit as a dictionary. For example:
data = {
'name': 'John Doe',
'occupation': 'gardener'
}
To make the POST request, call
r = http.request(
"POST",
"http://api.example.com/users",
fields=data
)
The
Check the
if r.status == 200:
print(r.data)
To post JSON data, use the
import json
data = json.dumps({'name': 'John Doe'})
r = http.request(
"POST",
"http://api.example.com/users",
body=data,
headers={'Content-Type': 'application/json'}
)