python request 发送formdata
时间: 2023-07-11 08:45:31 浏览: 134
使用 Python 中的 requests 库发送 formdata 可以按照以下步骤进行:
1. 导入 requests 库:
```python
import requests
```
2. 构建 formdata:
```python
data = {
'key1': 'value1',
'key2': 'value2',
'file': ('filename', open('path/to/file', 'rb'), 'filetype')
}
```
其中,`data` 是一个字典,包含了表单中的数据和文件。`('filename', open('path/to/file', 'rb'), 'filetype')` 是一个元组,分别表示文件名、文件内容和文件类型。
3. 发送请求:
```python
response = requests.post(url, data=data)
```
其中,`url` 是请求的 URL,`data` 是构建好的 formdata,使用 `post` 方法发送请求。
完整示例代码如下:
```python
import requests
data = {
'key1': 'value1',
'key2': 'value2',
'file': ('filename', open('path/to/file', 'rb'), 'filetype')
}
response = requests.post(url, data=data)
```
阅读全文
相关推荐


















