python 实现http带标准头域content-type=appliation/json
时间: 2024-02-26 20:56:41 浏览: 29
您可以使用 Python 中的 `requests` 库来发送 HTTP 请求并设置标准头域 `Content-Type: application/json`。以下是一个简单的示例代码:
```python
import requests
import json
# 定义请求头
headers = {'Content-Type': 'application/json'}
# 定义请求体
data = {'key': 'value'}
# 发送 POST 请求
response = requests.post(url, headers=headers, data=json.dumps(data))
# 打印响应结果
print(response.text)
```
在上述示例代码中,我们先定义了请求头 `Content-Type: application/json`,然后定义了请求体 `data`,最后使用 `requests.post()` 方法发送 POST 请求,并把请求头和请求体作为参数传递进去。`json.dumps()` 方法用于将 Python 对象转换为 JSON 格式的字符串。