xxxxxxxxxx
>>> import requests
>>> r = requests.post('http://httpbin.org/post', json={"key": "value"})
>>> r.status_code
200
>>> r.json()
{'args': {},
'data': '{"key": "value"}',
'files': {},
'form': {},
'headers': {'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate',
'Connection': 'close',
'Content-Length': '16',
'Content-Type': 'application/json',
'Host': 'httpbin.org',
'User-Agent': 'python-requests/2.4.3 CPython/3.4.0',
'X-Request-Id': 'xx-xx-xx'},
'json': {'key': 'value'},
'origin': 'x.x.x.x',
'url': 'http://httpbin.org/post'}
xxxxxxxxxx
import requests
import json
# Specify the url
url = 'http://httpbin.org/post'
# Create a dictionary with the data you want to send
data = {
'key1': 'value1',
'key2': 'value2'
}
# Send the POST request
response = requests.post(url, data=json.dumps(data))
# Check if the request was successful
if response.status_code == 200:
# Get the JSON data from the response
json_data = response.json()
# Process the JSON data (here, we just print it out)
print(json.dumps(json_data, indent=4))
else:
print(f'Request failed with status code {response.status_code}')