python requests post请求
时间: 2023-05-01 16:01:11 浏览: 133
使用 Python 的 requests 库发送 POST 请求非常简单,下面是一个示例代码:
```
import requests
url = 'http://example.com/api/endpoint'
data = {'key1': 'value1', 'key2': 'value2'}
headers = {'Content-type': 'application/json'}
response = requests.post(url, json=data, headers=headers)
print(response.status_code)
print(response.json())
```
这段代码会向 http://example.com/api/endpoint 发送一个 POST 请求,请求体包含 JSON 格式的 key1 和 key2,Content-type 头部设置为 application/json。
如果你想发送表单数据,可以使用 data 参数来代替 json 参数,并且 Content-type 头部设置为 application/x-www-form-urlencoded。
```
import requests
url = 'http://example.com/api/endpoint'
data = {'key1': 'value1', 'key2': 'value2'}
headers = {'Content-type': 'application/x-www-form-urlencoded'}
response = requests.post(url, data=data, headers=headers)
print(response.status_code)
print(response.json())
```
阅读全文
相关推荐

















