跳转到内容
Go back

Dify API 测试

更新于:

Dify API 测试

渗透测试某内部系统时,获取到 dify 平台的 api 和凭据信息

http://dify.com/v1/chat-messages
app-xxxx

需要编写一个测试脚本来测试获取到凭据信息是否有效

dify 官方的示例脚本运行报错,使用 deepseek 调整了一下,完整脚本如下

import requests
import json

url = "http://dify.com/v1/chat-messages"

headers = {
'Authorization': 'Bearer app-xxxx',
'Content-Type': 'application/json',
}


data = {
    "inputs": {},
    "query": "你是什么模型",
    "response_mode": "streaming",
    "user": "abc-123"
}

try:
    # 流式响应需要 stream=True
    response = requests.post(url, headers=headers, json=data, stream=True)
    
    if response.status_code == 200:
        print("开始接收流式响应:")
        for line in response.iter_lines():
            if line:
                # 解码并处理 SSE 格式数据
                line_str = line.decode('utf-8')
                if line_str.startswith('data: '):
                    data_str = line_str[6:]  # 移除 'data: ' 前缀
                    if data_str != '[DONE]':
                        try:
                            json_data = json.loads(data_str)
                            # 提取消息内容
                            if 'answer' in json_data:
                                print(json_data['answer'], end='', flush=True)
                        except json.JSONDecodeError:
                            pass
        print()  # 换行
    else:
        print(f"错误: {response.status_code}")
        print(response.text)
        
except Exception as e:
    print(f"发生错误: {e}")

参考资料


分享文章至:

Previous Post
写在 2025 年即将结束的时候
Next Post
SQL 注入全面检查 Workflow 记录