requests流式请求

import requests
import json

url = "https://api-dify.11111.cn/v1/chat-messages"

payload = json.dumps({
  "inputs": {},
  "query": "输入框写10条用例",
  "response_mode": "streaming",
  "conversation_id": "",
  "user": "abc-123",
  "url": "https://api-dify.1111.cn/v1/chat-messages"
})
headers = {
  'Authorization': 'Bearer 11111',
  'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data=payload, stream=True)
for line in response.iter_lines():  # 遍历响应内容的每一行
    if line:  # 确保这一行不是空的
        print(line.decode('utf-8'))  # 解码并打印输出

参考: https://blog.51cto.com/u_16175438/11827121

posted @ 2025-03-06 17:26  凯宾斯基  阅读(121)  评论(0)    收藏  举报