Skip to content

响应 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.

Runs once for the first data item

“回复 Webhook”节点运行一次,使用第一个传入的数据项。有关更多信息,请参阅 返回多个数据项

¥The Respond to Webhook node runs once, using the first incoming data item. Refer to Return more than one data item for more information.

如何使用 Webhook 响应#

¥How to use Respond to Webhook

要使用“响应 Webhook”节点:

¥To use the Respond to Webhook node:

  1. 添加一个 Webhook 节点作为工作流的触发节点。

¥Add a Webhook node as the trigger node for the workflow. 2. 在“Webhook”节点中,设置“响应于 '响应 Webhook' 节点”。

¥In the Webhook node, set Respond to Using 'Respond to Webhook' node. 3. 在工作流中的任意位置添加响应 Webhook 节点。如果你想从其他节点返回数据,请将其放在这些节点之后。

¥Add the Respond to Webhook node anywhere in your workflow. If you want it to return data from other nodes, place it after those nodes.

节点参数#

¥Node parameters

使用以下参数配置节点行为。

¥Configure the node behavior using these parameters.

使用响应#

¥Respond With

选择要在 Webhook 响应中发送的数据。

¥Choose what data to send in the webhook response.

  • 所有接收项目:使用输入中的所有 JSON 项进行响应。

¥All Incoming Items: Respond with all the JSON items from the input.

  • 二进制文件:使用响应数据源中定义的二进制文件进行响应。

¥Binary File: Respond with a binary file defined in Response Data Source.

  • 第一条传入数据:使用第一个传入项的 JSON 进行响应。

¥First Incoming Item: Respond with the first incoming item's JSON.

  • JSON:使用响应正文中定义的 JSON 对象进行响应。

¥JSON: Respond with a JSON object defined in Response Body.

  • JWT 令牌:使用 JSON Web Token (JWT) 进行响应。

¥JWT Token: Respond with a JSON Web Token (JWT).

  • 无数据:无响应负载。

¥No Data: No response payload.

  • 重定向:重定向到“重定向 URL”中设置的 URL。

¥Redirect: Redirect to a URL set in Redirect URL.

  • 文本:使用响应正文中设置的文本进行响应。默认情况下,此操作会发送 HTML (Content-Type: text/html)。

¥Text: Respond with text set in Response Body. This sends HTML by default (Content-Type: text/html).

节点选项#

¥Node options

选择“添加选项”以查看和设置选项。

¥Select Add Option to view and set the options.

¥Response Code: Set the response code to use.

  • 响应标头:定义要发送的响应标头。

¥Response Headers: Define the response headers to send.

  • 将响应放入字段:当你回复“所有传入项目”或“第一个传入项目”时可用。设置包含响应数据的字段名称。

¥Put Response in Field: Available when you respond with All Incoming Items or First Incoming Item. Set the field name for the field containing the response data.

  • 启用流式传输:启用后,使用流式传输将数据发送回用户。需要配置一个响应模式为“流式”的触发器。

¥Enable Streaming: When enabled, sends the data back to the user using streaming. Requires a trigger configured with the Response mode Streaming.

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 中渲染,而不是直接在父文档中渲染。

¥HTML renders in a sandboxed iframe instead of directly in the parent document.

  • 尝试访问顶层窗口或本地存储的 JavaScript 代码将会失败。

¥JavaScript code that attempts to access the top-level window or local storage will fail.

  • 沙盒 iframe 中不提供身份验证标头(例如,基本身份验证)。你需要使用其他方法,例如在 HTML 中嵌入一个短期访问令牌。

¥Authentication headers aren't available in the sandboxed iframe (for example, basic auth). You need to use an alternative approach, like embedding a short-lived access token within the HTML.

  • 相对 URL(例如 <form action="/">)将无法使用。改用绝对 URL。

¥Relative URLs (for example, <form action="/">) won't work. Use absolute URLs instead.

模板和示例#

¥Templates and examples

Template widget placeholder.

工作流行为#

¥Workflow behavior

使用“响应 Webhook”节点时,工作流的行为如下:

¥When using the Respond to Webhook node, workflows behave as follows:

  • 工作流在未执行“响应 Webhook”节点的情况下完成:它返回状态码为 200 的标准消息。

¥The workflow finishes without executing the Respond to Webhook node: it returns a standard message with a 200 status.

  • 工作流在第一个“响应 Webhook”节点执行之前出错:工作流返回状态码为 500 的错误消息。

¥The workflow errors before the first Respond to Webhook node executes: the workflow returns an error message with a 500 status.

  • 第二个响应 Webhook 节点在第一个节点执行后执行:工作流会忽略它。

¥A second Respond to Webhook node executes after the first one: the workflow ignores it.

  • 响应 Webhook 节点已执行,但没有 Webhook:工作流忽略“响应 Webhook”节点。

¥A Respond to Webhook node executes but there was no webhook: the workflow ignores the Respond to Webhook node.

输出发送到 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:

  • 输入数据:原始输出,传递节点的输入。

¥Input Data: The original output, passing on the node's input.

  • 响应:发送到 Webhook 的响应对象。

¥Response: The response object sent to the webhook.

返回多个数据项(已弃用)#

¥Return more than one data item (deprecated)

Deprecated in 1.22.0

n8n 1.22.0 添加了使用“所有传入项”选项返回所有数据项的支持。n8n 建议升级到最新版本的 n8n,而不是使用本节中描述的变通方法。

¥n8n 1.22.0 added support for returning all data items using the All Incoming Items option. n8n recommends upgrading to the latest version of n8n, instead of using the workarounds described in this section.

“回复 Webhook”节点运行一次,使用第一个传入的数据项。这包括使用 expressions 的情况。无法使用“循环”节点强制循环。工作流将运行,但 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:

  • 与其使用“响应 Webhook”节点,不如使用 Webhook 节点中“响应”的“当最后一个节点完成时”选项。当希望返回工作流输出的最终数据时,请使用此选项。

¥Instead of using the Respond to Webhook node, use the When Last Node Finishes option in Respond in the Webhook node. Use this when you want to return the final data that the workflow outputs.

  • 使用 聚合 节点将多个项目合并为一个项目,然后再将数据传递给“响应 Webhook”节点。将“聚合”设置为“所有项目数据(汇总到单个列表中)”。

¥Use the Aggregate node to turn multiple items into a single item before passing the data to the Respond to Webhook node. Set Aggregate to All Item Data (Into a Single List).