agentic_huge_data_base / wiki
页面 Dify · 5.4 HTTP 请求与代码执行节点·DeepWiki 中文全文译文

5.4 · HTTP 请求与代码执行节点(HTTP Request and Code Execution Nodes)

应用编排与外部知识接入 · 聚焦本章的模块关系、源码依据与实现要点。

项目Dify 章节5.4 状态全文译文 模块认证、权限与安全、接口与服务契约、配置治理、测试、发布与运维
源码线索
  • api/.importlinter
  • api/core/app/file_access/__init__.py
  • api/core/app/file_access/controller.py
  • api/core/app/file_access/scope.py
  • api/core/helper/code_executor/__init__.py
  • api/core/helper/code_executor/code_executor.py
  • api/core/helper/code_executor/code_node_provider.py
  • api/core/helper/code_executor/javascript/__init__.py
  • api/core/helper/code_executor/javascript/javascript_code_provider.py
  • api/core/helper/code_executor/javascript/javascript_transformer.py
模块标签
  • 认证、权限与安全
  • 接口与服务契约
  • 配置治理
  • 测试、发布与运维
  • 界面与交互

章节正文

HTTP 请求与代码执行节点

HTTP 请求与代码执行节点

相关源文件

以下文件为本 Wiki 页面的生成提供了上下文:

  • api/.importlinter
  • api/core/app/file_access/__init__.py
  • api/core/app/file_access/controller.py
  • api/core/app/file_access/scope.py
  • api/core/helper/code_executor/__init__.py
  • api/core/helper/code_executor/code_executor.py
  • api/core/helper/code_executor/code_node_provider.py
  • api/core/helper/code_executor/javascript/__init__.py
  • api/core/helper/code_executor/javascript/javascript_code_provider.py
  • api/core/helper/code_executor/javascript/javascript_transformer.py
  • api/core/helper/code_executor/jinja2/__init__.py
  • api/core/helper/code_executor/jinja2/jinja2_formatter.py
  • api/core/helper/code_executor/jinja2/jinja2_transformer.py
  • api/core/helper/code_executor/python3/__init__.py
  • api/core/helper/code_executor/python3/python3_transformer.py
  • api/core/helper/code_executor/template_transformer.py
  • api/core/workflow/node_factory.py
  • api/core/workflow/node_runtime.py
  • api/tests/integration_tests/workflow/nodes/__mock/code_executor.py
  • api/tests/integration_tests/workflow/nodes/code_executor/test_code_javascript.py
  • api/tests/integration_tests/workflow/nodes/test_code.py
  • api/tests/integration_tests/workflow/nodes/test_http.py
  • api/tests/integration_tests/workflow/nodes/test_llm.py
  • api/tests/integration_tests/workflow/nodes/test_parameter_extractor.py
  • api/tests/integration_tests/workflow/nodes/test_template_transform.py
  • api/tests/integration_tests/workflow/nodes/test_tool.py
  • api/tests/test_containers_integration_tests/__init__.py
  • api/tests/test_containers_integration_tests/factories/__init__.py
  • api/tests/test_containers_integration_tests/workflow/__init__.py
  • api/tests/test_containers_integration_tests/workflow/nodes/__init__.py
  • api/tests/test_containers_integration_tests/workflow/nodes/code_executor/__init__.py
  • api/tests/test_containers_integration_tests/workflow/nodes/code_executor/test_code_executor.py
  • api/tests/test_containers_integration_tests/workflow/nodes/code_executor/test_code_javascript.py
  • api/tests/test_containers_integration_tests/workflow/nodes/code_executor/test_code_jinja2.py
  • api/tests/test_containers_integration_tests/workflow/nodes/code_executor/test_code_python3.py
  • api/tests/unit_tests/core/helper/code_executor/__init__.py
  • api/tests/unit_tests/core/helper/code_executor/javascript/__init__.py
  • api/tests/unit_tests/core/helper/code_executor/javascript/test_javascript_transformer.py
  • api/tests/unit_tests/core/helper/code_executor/python3/__init__.py
  • api/tests/unit_tests/core/helper/code_executor/python3/test_python3_transformer.py
  • api/tests/unit_tests/core/rag/datasource/test_retrieval_attachment_access.py
  • api/tests/unit_tests/core/workflow/graph_engine/test_mock_factory.py
  • api/tests/unit_tests/core/workflow/graph_engine/test_mock_nodes.py
  • api/tests/unit_tests/core/workflow/graph_engine/test_tool_in_chatflow.py
  • api/tests/unit_tests/core/workflow/nodes/answer/test_answer.py
  • api/tests/unit_tests/core/workflow/nodes/code/__init__.py
  • api/tests/unit_tests/core/workflow/nodes/code/code_node_spec.py
  • api/tests/unit_tests/core/workflow/nodes/http_request/test_http_request_executor.py
  • api/tests/unit_tests/core/workflow/nodes/http_request/test_http_request_node.py
  • api/tests/unit_tests/core/workflow/nodes/template_transform/__init__.py
  • api/tests/unit_tests/core/workflow/nodes/template_transform/template_transform_node_spec.py
  • api/tests/unit_tests/core/workflow/nodes/test_if_else.py
  • api/tests/unit_tests/core/workflow/nodes/test_list_operator.py
  • api/tests/unit_tests/core/workflow/nodes/tool/test_tool_node.py
  • api/tests/unit_tests/core/workflow/test_node_factory.py
  • api/tests/unit_tests/core/workflow/test_node_runtime.py

本文档介绍 Dify 工作流引擎中的 HTTP 请求节点和代码执行节点。这些节点使工作流能够通过 HTTP 与外部服务交互,并在沙箱环境中执行自定义代码逻辑。

HTTP 请求节点架构

HTTP 请求节点支持工作流向外部 API 发起 HTTP 请求,具备模板变量替换、多种认证方式以及全面的安全控制能力。

组件结构
Dify · 组件结构 · 图 1
Dify · 组件结构 · 图 1

HTTP 请求节点组件架构

来源:api/tests/unit_tests/core/workflow/nodes/http_request/test_http_request_executor.py:7-16, api/tests/unit_tests/core/workflow/nodes/http_request/test_http_request_node.py:14-16, api/tests/unit_tests/core/workflow/nodes/http_request/test_http_request_node.py:119-129

配置与初始化

HTTP 请求节点通过 HttpRequestNodeConfig(定义系统级限制)和 HttpRequestNodeData(包含每个节点的配置)进行配置。

配置参数类型用途默认值
max_connect_timeoutint最大连接超时时间(秒)dify_config.HTTP_REQUEST_MAX_CONNECT_TIMEOUT
max_read_timeoutint最大读取超时时间(秒)dify_config.HTTP_REQUEST_MAX_READ_TIMEOUT
max_write_timeoutint最大写入超时时间(秒)dify_config.HTTP_REQUEST_MAX_WRITE_TIMEOUT
max_binary_sizeint最大二进制响应大小(字节)dify_config.HTTP_REQUEST_NODE_MAX_BINARY_SIZE
max_text_sizeint最大文本响应大小(字节)dify_config.HTTP_REQUEST_NODE_MAX_TEXT_SIZE
ssl_verifybool启用 SSL 证书验证dify_config.HTTP_REQUEST_NODE_SSL_VERIFY
ssrf_default_max_retriesintSSRF 代理的最大重试次数dify_config.SSRF_DEFAULT_MAX_RETRIES

来源:api/tests/unit_tests/core/workflow/nodes/http_request/test_http_request_executor.py:19-27, api/tests/unit_tests/core/workflow/nodes/http_request/test_http_request_node.py:19-27

模板变量替换

HTTP 请求节点支持在 URL、请求头、参数和请求体中使用 {{#node_id.variable#}} 语法进行模板变量替换。

变量解析流程
Dify · 变量解析流程 · 图 2
Dify · 变量解析流程 · 图 2

模板变量解析管线

支持的变量位置
位置模板支持示例
URLhttps://api.example.com/data
请求头Content-Type: application/json
查询参数A:b
JSON 请求体{"number": {{#pre_node_id.number#}}}
模板处理示例

包含对象变量的 JSON 请求体:

# 来自 test_executor_with_json_body_and_object_variable
node_data = HttpRequestNodeData(
    method="post",
    url="https://api.example.com/data",
    body=HttpRequestNodeBody(
        type="json",
        data=[BodyData(key="", type="text", value="{{#pre_node_id.object#}}")]
    ),
    ...
)

Executor 类会自动处理以下情况:

  • 基本类型:替换到 JSON 模板中。api/tests/unit_tests/core/workflow/nodes/http_request/test_http_request_executor.py:30-56
  • 对象:引用时自动序列化为 JSON 对象。api/tests/unit_tests/core/workflow/nodes/http_request/test_http_request_executor.py:92-112
  • 嵌套结构:通过 VariablePool 解析深层嵌套的对象。api/tests/unit_tests/core/workflow/nodes/http_request/test_http_request_executor.py:144-180

来源:api/tests/unit_tests/core/workflow/nodes/http_request/test_http_request_executor.py:30-84, api/tests/unit_tests/core/workflow/nodes/http_request/test_http_request_executor.py:86-142

认证方式

HTTP 请求节点通过 HttpRequestNodeAuthorization 支持多种认证类型:

认证类型请求头名称配置的请求头值
no-auth不适用
api-key(基本认证)用户自定义Basic {api_key}
api-key(自定义)用户自定义{api_key}

来源:api/tests/integration_tests/workflow/nodes/test_http.py:103-110, api/tests/integration_tests/workflow/nodes/test_http.py:137-140, api/tests/integration_tests/workflow/nodes/test_http.py:167-174

代码执行节点架构

代码节点支持在工作流中执行 Python、JavaScript 或 Jinja2 代码。它使用基于模板的转换系统,将用户代码准备好后发送到远程沙箱执行。

组件结构
Dify · 组件结构 · 图 3
Dify · 组件结构 · 图 3

代码执行架构

来源:api/core/helper/code_executor/code_executor.py:50-58, api/core/helper/code_executor/code_executor.py:77-79, api/core/helper/code_executor/template_transformer.py:11-15

沙箱集成

Dify 与专用的沙箱服务通信以执行代码。请求中包含代码、预加载脚本(用于环境设置)以及目标语言。

# 来自 CodeExecutor.execute_code
data = {
    "language": cls.code_language_to_running_language.get(language),
    "code": code,
    "preload": preload,
    "enable_network": True,
}
headers = {"X-Api-Key": dify_config.CODE_EXECUTION_API_KEY}

来源:api/core/helper/code_executor/code_executor.py:77-86

模板转换

在将代码发送到沙箱之前,系统会使用 TemplateTransformer 对代码进行转换,以包含输入变量并捕获输出。

  1. 序列化:输入和代码经过 base64 编码,以防止 shell 注入或语法破坏。api/core/helper/code_executor/template_transformer.py:17-23
  2. 运行器脚本:生成一个包装脚本(例如 Python 或 Node.js),该脚本解码输入、调用用户的 main 函数,并在 <<RESULT>> 标签之间打印结果。api/core/helper/code_executor/template_transformer.py:110-116
  3. 响应解析:执行器提取标签之间的字符串,并将其解析回 Python 字典。api/core/helper/code_executor/template_transformer.py:39-43

来源:api/core/helper/code_executor/template_transformer.py:26-36, api/core/helper/code_executor/code_executor.py:144-150

安全与连接

SSRF 保护(ssrf_proxy

ssrf_proxy 模块提供了一种集中式方式来发起出站 HTTP 请求,同时防止服务端请求伪造(SSRF)。它通过 DifyNodeFactory 注入到节点中。

  • 客户端池化:使用 get_pooled_http_client 管理连接生命周期。api/core/helper/code_executor/code_executor.py:15-16, api/core/helper/code_executor/code_executor.py:95
  • SSRF 处理graphon_ssrf_proxyDifyNodeFactory 中使用,为节点提供安全的 HTTP 客户端。api/core/workflow/node_factory.py:18, api/core/workflow/node_factory.py:18-87

来源:api/core/workflow/node_factory.py:18-87

代码沙箱限制

CodeNode 对沙箱返回的数据施加限制,以防止内存耗尽或数据传输过多。这些限制在 CodeNodeLimits 中定义。

限制项配置变量用途
max_string_lengthCODE_MAX_STRING_LENGTH输出中任何单个字符串的最大长度
max_numberCODE_MAX_NUMBER最大数值
max_depthCODE_MAX_DEPTH对象/数组的最大嵌套深度
max_precisionCODE_MAX_PRECISION数字的最大小数精度

来源:api/core/workflow/node_factory.py:52

测试与模拟

系统包含针对这两种节点类型的广泛集成测试,使用了模拟工厂和提供者。

  • HTTP 模拟:使用 setup_http_mock 在集成测试中验证 GET/POST 行为和请求头处理。api/tests/integration_tests/workflow/nodes/test_http.py:92-93
  • 认证验证:专门测试自定义认证模式在缺少凭证时是否会引发 AuthorizationConfigErrorapi/tests/integration_tests/workflow/nodes/test_http.py:193-202
  • 响应解析:测试确保 Executor 能够正确地从 VariablePool 解析 JSON 请求体,包括嵌套结构。api/tests/unit_tests/core/workflow/nodes/http_request/test_http_request_executor.py:144-180
  • 节点工厂集成DifyNodeFactory 负责实例化这些节点,并注入所需的依赖项,如 HttpClientProtocolToolFileManagerapi/core/workflow/node_factory.py:24-33

来源:api/tests/integration_tests/workflow/nodes/test_http.py:92-202, api/tests/unit_tests/core/workflow/nodes/http_request/test_http_request_executor.py:144-180, api/core/workflow/node_factory.py:24-33