python requests post语句
时间: 2023-12-13 07:04:31 浏览: 77
Python的requests库提供了post方法来发送POST请求。下面是post方法的基本语法:
```python
import requests
response = requests.post(url, data=data, json=json_data, headers=headers)
```
参数说明:
- `url`:要发送POST请求的URL。
- `data`:要发送的数据,可以是字典、字符串、字节流或文件对象。
- `json`:要发送的JSON数据,可以是字典或JSON字符串。
- `headers`:请求头信息,包括Content-Type、User-Agent等。
示例代码:
```python
import requests
# 发送POST请求,携带表单数据
url = 'http://example.com/api'
data = {'username': 'foo', 'password': 'bar'}
response = requests.post(url, data=data)
# 发送POST请求,携带JSON数据
url = 'http://example.com/api'
json_data = {'name': 'John', 'age': 30}
response = requests.post(url, json=json_data)
# 发送POST请求,自定义请求头
url = 'http://example.com/api'
data = {'key1': 'value1', 'key2': 'value2'}
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
response = requests.post(url, data=data, headers=headers)
```
阅读全文
相关推荐













