将以下curl命令转为python调用语句 curl -X POST 'http://10.97.38.129/v1/chat-messages' --header 'Authorization: Bearer {api_key}' --header 'Content-Type: application/json' --data-raw '{ "inputs": {}, "query": "What are the specs of the iPhone 13 Pro Max?", "response_mode": "streaming", "conversation_id": "", "user": "abc-123", "files": [ { "type": "image", "transfer_method": "remote_url", "url": "https://cloud.dify.ai/logo/logo-site.png" } ] }'
时间: 2025-06-01 10:12:49 浏览: 22
### 将 `curl` 命令转换为等效的 Python `requests` 库代码
以下是使用 Python 的 `requests` 库实现发送 POST 请求到指定 URL,并包含 `Authorization` 头、`Content-Type` 头以及 JSON 数据体的示例代码:
```python
import requests
import json
# 定义URL
url = 'http://10.97.38.129/v1/chat-messages'
# 定义请求头
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_ACCESS_TOKEN" # 替换为实际的授权令牌
}
# 定义JSON数据体
data = {
"event": "message",
"task_id": "853317cc-0fab-456f-b90c-3c07918c3cb3",
"id": "19c6c9d7-3228-4110-beaf-5cb42fbe0b99",
"message_id": "19c6c9d7-3228-4110-beaf-5cb42fbe0b99",
"conversation_id": "12be1265-f5e4-43cf-b7ac-5d2f1272f649",
"mode": "chat",
"answer": "你好!很高兴见到你。有什么我可以帮忙的吗?或者你最近有什么想聊的话题吗? 😊",
"metadata": {
"usage": {
"prompt_tokens": 4,
"prompt_unit_price": "0",
"prompt_price_unit": "0",
"prompt_price": "0",
"completion_tokens": 23,
"completion_unit_price": "0",
"completion_price_unit": "0",
"completion_price": "0",
"total_tokens": 27,
"total_price": "0",
"currency": "USD",
"latency": 1.2631837779999842
}
},
"created_at": 1747465469
}
# 发送POST请求
response = requests.post(url, headers=headers, json=data)
# 输出响应结果
print("响应状态码:", response.status_code)
print("响应内容:", response.text)
```
上述代码实现了以下功能:
1. 使用 `requests.post` 方法发送 POST 请求。
2. 设置了 `Content-Type` 和 `Authorization` 请求头[^2]。
3. 将 JSON 数据体通过 `json` 参数传递给 `requests.post` 方法,确保数据被正确序列化为 JSON 格式[^3]。
### 注意事项
- 确保替换 `YOUR_ACCESS_TOKEN` 为实际的授权令牌。
- 如果目标服务器需要其他特定参数或配置,请根据实际情况调整代码。
阅读全文
相关推荐


















