以下是使用requests
库调用HTTP接口进行POST请求的15个示例:
- 发送简单的POST请求:
import requestspayload = { 'key1': 'value1', 'key2': 'value2'}response = requests.post('http://example.com', data=payload)print(response.text)
- 发送JSON格式的POST请求:
import requestsimport jsonpayload = { 'key1': 'value1', 'key2': 'value2'}response = requests.post('http://example.com', json=json.dumps(payload))print(response.text)
- 发送XML格式的POST请求:
import requestspayload = 'value1 value2 'response = requests.post('http://example.com', data=payload, headers={ 'Content-Type': 'application/xml'})print(response.text)
- 发送文件的POST请求:
import requestsfiles = { 'file': open('file.txt', 'rb')}response = requests.<