响应 Webhook(Respond to Webhook)#
使用“Respond to Webhook”节点来控制对传入 webhook 的响应。该节点可与Webhook节点配合使用。
🌐 Use the Respond to Webhook node to control the response to incoming webhooks. This node works with the Webhook node.
首次对第一个数据项运行一次
响应 Webhook 的节点只运行一次,使用第一个传入的数据项。有关更多信息,请参阅 返回多个数据项。
如何使用 Webhook 响应(How to use Respond to Webhook)#
要使用“响应 Webhook”节点:
🌐 To use the Respond to Webhook node:
- 添加一个 Webhook 节点作为工作流的触发节点。
- 在 Webhook 节点中,将 Respond 设置为 使用 'Respond to Webhook' 节点。
- 在工作流中的任意位置添加“响应到 Webhook”节点。如果你希望它返回来自其他节点的数据,请将其放在那些节点之后。
节点参数(Node parameters)#
使用以下参数配置节点行为。
🌐 Configure the node behavior using these parameters.
使用响应(Respond With)#
选择要在 Webhook 响应中发送的数据。
🌐 Choose what data to send in the webhook response.
- 所有传入项目:回应输入中的所有 JSON 项目。
- 二进制文件:请回复 响应数据源 中定义的二进制文件。
- 首个到达的项目:请用首个到达项目的 JSON 作出回应。
- JSON:以响应正文中定义的 JSON 对象进行响应。
- JWT 令牌:返回一个 JSON Web 令牌(JWT)。
- 无数据:无响应内容。
- 重定向:重定向到在 重定向 URL 中设置的 URL。
- 文本:请在响应正文中以文本形式回复。默认情况下,这会发送 HTML (
Content-Type: text/html)。
节点选项(Node options)#
选择 添加选项 以查看和设置选项。
🌐 Select Add Option to view and set the options.
- 响应代码:设置要使用的响应代码。
- 响应头:定义要发送的响应头。
- 将响应放入字段:当你选择 所有传入项目 或 第一个传入项目 响应时可用。设置包含响应数据的字段名称。
- 启用流式传输:启用后,通过流式传输将数据发送回用户。需要配置触发器,并将响应模式设置为流式传输。
n8n 如何保护 HTML 响应(How n8n secures HTML responses)#
从 n8n 版本 1.103.0 开始,n8n 会自动将返回给 webhook 的 HTML 响应封装在 <iframe> 标签中。这是一种保护实例用户的安全机制。
🌐 Starting with n8n version 1.103.0, n8n automatically wraps HTML responses to webhooks in <iframe> tags. This is a security mechanism to protect the instance users.
这具有以下意义:
🌐 This has the following implications:
- HTML 在沙盒化的 iframe 中渲染,而不是直接在父文档中渲染。
- 尝试访问顶层窗口或本地存储的 JavaScript 代码将会失败。
- 在沙箱化的 iframe 中无法使用身份验证头(例如,基本身份验证)。你需要使用其他方法,例如在 HTML 中嵌入短期有效的访问令牌。
- 相对 URL(例如
<form action="/">)无法使用。请改用绝对 URL。
模板和示例(Templates and examples)#
工作流行为(Workflow behavior)#
使用“响应 Webhook”节点时,工作流的行为如下:
🌐 When using the Respond to Webhook node, workflows behave as follows:
- 工作流在未执行“响应 Webhook”节点的情况下完成:它返回一个带有 200 状态的标准消息。
- 在第一个“响应 Webhook”节点执行之前,工作流就出现错误:工作流返回了一个带有 500 状态的错误信息。
- 第二个响应 Webhook 节点在第一个节点之后执行:工作流会忽略它。
- 响应 Webhook 节点已执行,但没有 webhook:工作流会忽略响应 Webhook 节点。
输出发送到 Webhook 的响应(Output the response sent to the webhook)#
默认情况下,“响应 Webhook”节点只有一个输出分支,其中包含该节点的输入数据。
🌐 By default, the Respond to Webhook node has a single output branch that contains the node's input data.
您可以选择启用包含发送到 Webhook 的响应的第二个输出分支。要启用此辅助输出,请在画布上打开“响应 Webhook”节点并选择 设置 选项卡。激活 启用响应输出分支 选项。
🌐 You can optionally enable a second output branch containing the response sent to the webhook. To enable this secondary output, open the Respond to Webhook node on the canvas and select the Settings tab. Activate the Enable Response Output Branch option.
节点现在将有两个输出:
🌐 The node will now have two outputs:
- 输入数据:原始输出,传递节点的输入。
- 响应:发送到 Webhook 的响应对象。
返回多个数据项(已弃用)(Return more than one data item (deprecated))#
在 1.22.0 中弃用
n8n 1.22.0 增加了使用 所有传入项目 选项返回所有数据项的支持。n8n 建议升级到最新版本,而不是使用本节中描述的变通方法。
Respond to Webhook 节点只运行一次,使用第一个传入的数据项。这也包括使用表达式的情况。你无法使用 Loop 节点强制循环:工作流会运行,但 webhook 响应仍然只会包含第一次执行的结果。
🌐 The Respond to Webhook node runs once, using the first incoming data item. This includes when using expressions. You can't force looping using the Loop node: the workflow will run, but the webhook response will still only contain the results of the first execution.
如果你需要返回多个数据项,请选择以下选项之一:
🌐 If you need to return more than one data item, choose one of these options:
- 不要使用“Respond to Webhook”节点,而是在Webhook节点的“Respond”中使用“当最后一个节点完成时”选项。当你想要返回工作流输出的最终数据时,请使用此选项。
- 使用 Aggregate 节点将多个项目合并为一个项目,然后再将数据传递给 Respond to Webhook 节点。将 Aggregate 设置为 所有项目数据(合并为单个列表)。