HTTP 请求节点(HTTP Request node)#
HTTP 请求节点是 n8n 中最通用的节点之一。它允许你发起 HTTP 请求,从任何具有 REST API 的应用或服务中查询数据。你可以将 HTTP 请求节点作为常规节点使用,也可以连接到 AI 代理 作为 工具 使用。
🌐 The HTTP Request node is one of the most versatile nodes in n8n. It allows you to make HTTP requests to query data from any app or service with a REST API. You can use the HTTP Request node a regular node or attached to an AI agent to use as a tool.
使用此节点时,你正在创建一个 REST API 调用。你需要对基本的 API 术语和概念有一定的了解。
🌐 When using this node, you're creating a REST API call. You need some understanding of basic API terminology and concepts.
有两种方法可以创建 HTTP 请求:配置节点参数或导入 curl 命令。
🌐 There are two ways to create an HTTP request: configure the node parameters or import a curl command.
凭证
请参阅 HTTP 请求凭据 以获取有关设置身份验证的指南。
节点参数(Node parameters)#
方法(Method)#
选择用于请求的方法:
🌐 Select the method to use for the request:
- 删除
- 获取
- 头部
- 选项
- 补丁
- 发布
- 放置
网址(URL)#
输入你要使用的端点。
🌐 Enter the endpoint you want to use.
验证(Authentication)#
n8n 建议在可用时使用 预定义凭证类型 选项。与配置通用凭证相比,它提供了一种更容易设置和管理凭证的方式。
🌐 n8n recommends using the Predefined Credential Type option when it's available. It offers an easier way to set up and manage credentials, compared to configuring generic credentials.
预定义凭据(Predefined credentials)#
n8n 支持的集成凭证,包括内置节点和社区节点。对于无需额外设置的自定义操作,请使用 预定义凭证类型。更多信息请参阅 自定义 API 操作。
🌐 Credentials for integrations supported by n8n, including both built-in and community nodes. Use Predefined Credential Type for custom operations without extra setup. Refer to Custom API operations for more information.
通用凭据(Generic credentials)#
n8n 不支持的集成凭证。你需要手动配置身份验证流程,包括指定所需的 API 端点、必要的参数以及身份验证方法。
🌐 Credentials for integrations not supported by n8n. You'll need to manually configure the authentication process, including specifying the required API endpoints, necessary parameters, and the authentication method.
你可以选择以下方法之一:
🌐 You can select one of the following methods:
- 基本身份验证
- 自定义身份验证
- 摘要式身份验证
- 标头身份验证
- OAuth1 API
- OAuth2 API
- 查询身份验证
有关设置每种凭据类型的更多信息,请参考 HTTP 请求凭据。
🌐 Refer to HTTP request credentials for more information on setting up each credential type.
发送查询参数(Send Query Parameters)#
查询参数作为 HTTP 请求的过滤器。如果你使用的 API 支持查询参数,并且你发出的请求需要过滤器,请开启此选项。
🌐 Query parameters act as filters on HTTP requests. If the API you're interacting with supports them and the request you're making needs a filter, turn this option on.
使用以下可用选项 指定你的查询参数:
- 使用以下字段:输入 名称/值 对的 查询参数。要输入更多查询参数名称/值对,请选择 添加参数。名称是你要过滤的字段名称,值是过滤值。
- 使用 JSON:输入 JSON 来定义你的查询参数。
请参阅你服务的 API 文档以获取详细指南。
🌐 Refer to your service's API documentation for detailed guidance.
发送邮件头(Send Headers)#
使用此参数可以在请求中发送头信息。头信息包含有关请求的元数据或上下文。
🌐 Use this parameter to send headers with your request. Headers contain metadata or context about your request.
指定标题,使用以下可用选项之一:
- 使用以下字段:输入名称/值对作为头部参数。要输入更多的头部参数名称/值对,请选择添加参数。名称是你希望设置的头部,值是你想要传递的该头部的值。
- 使用 JSON:输入 JSON 来定义你的头部参数。
请参阅你服务的 API 文档以获取详细指南。
🌐 Refer to your service's API documentation for detailed guidance.
发送正文(Send Body)#
如果你需要在 API 请求中发送请求体,请启用此选项。
🌐 If you need to send a body with your API request, turn this option on.
然后选择最适合你希望发送的正文内容格式的正文内容类型。
🌐 Then select the Body Content Type that best matches the format for the body content you wish to send.
表单 URL 编码(Form URLencoded)#
使用此选项将你的主体作为 application/x-www-form-urlencoded 发送。
🌐 Use this option to send your body as application/x-www-form-urlencoded.
指定正文,使用以下可用选项之一:
- 使用以下字段:输入 Body 参数 的 名称/值 对。要输入更多的 Body 参数名称/值对,请选择 添加参数。名称应为表单字段名称,值为你希望设置该字段的内容。
- 使用单个字段:在单个 Body 参数中输入你的名称/值对,格式为
fieldname1=value1&fieldname2=value2。
请参阅你服务的 API 文档以获取详细指南。
🌐 Refer to your service's API documentation for detailed guidance.
表单数据(Form-Data)#
使用此选项将你的主体作为 multipart/form-data 发送。
🌐 Use this option to send your body as multipart/form-data.
通过选择 参数类型 配置你的 主体参数:
🌐 Configure your Body Parameters by selecting the Parameter Type:
- 选择 表单数据 来输入 名称/数值 对。
- 选择 n8n 二进制文件 从节点可以访问的文件中提取内容。
- 名称:输入要设置的字段ID。
- 输入数据字段名称:输入包含你要处理的二进制文件数据的传入字段的名称。
选择 添加参数 来输入更多参数。
🌐 Select Add Parameter to enter more parameters.
请参阅你服务的 API 文档以获取详细指南。
🌐 Refer to your service's API documentation for detailed guidance.
JSON#
使用此选项可将正文以 JSON 格式发送。
🌐 Use this option to send your body as JSON.
指定正文,使用以下可用选项之一:
- 使用以下字段:输入 Body 参数 的 名称/值 对。要输入更多 Body 参数的名称/值对,请选择 添加参数。
- 使用 JSON:输入 JSON 来定义你的请求体。
请参阅你服务的 API 文档以获取详细指南。
🌐 Refer to your service's API documentation for detailed guidance.
n8n 二进制文件(n8n Binary File)#
使用此选项可将存储在 n8n 中的文件内容作为正文发送。
🌐 Use this option to send the contents of a file stored in n8n as the body.
输入包含文件的传入字段的名称作为输入数据字段名称。
🌐 Enter the name of the incoming field that contains the file as the Input Data Field Name.
有关如何格式化文件的详细指南,请参阅你服务的 API 文档。
🌐 Refer to your service's API documentation for detailed guidance on how to format the file.
Raw#
使用此选项可在请求体中发送原始数据。
🌐 Use this option to send raw data in the body.
- 内容类型:输入
Content-Type头以用于原始正文内容。有关完整的 MIME 内容类型列表,请参阅 IANA 媒体类型 文档。 - 正文:输入要发送的原始正文内容。
请参阅你服务的 API 文档以获取详细指南。
🌐 Refer to your service's API documentation for detailed guidance.
节点选项(Node options)#
选择 添加选项 以查看并选择这些选项。除非另有说明,所有参数均可使用这些选项。
🌐 Select Add Option to view and select these options. Options are available to all parameters unless otherwise noted.
查询中的数组格式参数(Array Format in Query Parameters)#
选项可用性
此选项仅在你启用 发送查询参数 时可用。
使用此选项来控制查询参数中包含的数组的格式。可从以下选项中选择:
🌐 Use this option to control the format for arrays included in query parameters. Choose from these options:
- 无括号:数组将以 name=value 的形式格式化每个数组项,例如:
foo=bar&foo=qux。 - 仅限括号:该节点会在每个数组名称后添加方括号,例如:
foo[]=bar&foo[]=qux。 - 带索引的方括号:该节点在每个数组名称后添加带索引值的方括号,例如:
foo[0]=bar&foo[1]=qux。
请参阅你服务的 API 文档以获取有关使用哪个选项的指南。
🌐 Refer to your service's API documentation for guidance on which option to use.
批处理(Batching)#
控制大量输入项的批处理方式:
🌐 Control how to batch large numbers of input items:
- 每批项目数:输入每批要包含的项目数量。
- 批处理间隔:输入每批请求之间等待的时间(以毫秒为单位)。输入 0 表示没有批处理间隔。
忽略 SSL 问题(Ignore SSL Issues)#
默认情况下,n8n 仅在 SSL 证书验证成功时下载响应。如果你希望即使 SSL 证书验证失败也能下载响应,请开启此选项。
🌐 By default, n8n only downloads the response if SSL certificate validation succeeds. If you'd like to download the response even if SSL certificate validation fails, turn this option on.
小写标头(Lowercase Headers)#
选择是否将邮件头名称转换为小写(已开启,默认)或不转换为小写(已关闭)。
🌐 Choose whether to lowercase header names (turned on, default) or not (turned off).
重定向(Redirects)#
选择是否跟随重定向(默认开启)或不跟随(默认关闭)。如果开启,请在 最大重定向次数 中输入请求应跟随的最大重定向次数。
🌐 Choose whether to follow redirects (turned on by default) or not (turned off). If turned on, enter the maximum number of redirects the request should follow in Max Redirects.
响应(Response)#
使用此选项可设置预期 API 响应的一些详细信息,包括:
🌐 Use this option to set some details about the expected API response, including:
- 包含响应头和状态:默认情况下,节点仅返回主体。打开此选项可返回完整响应(包括头信息和响应状态码)以及主体内容。
- 永不出错:默认情况下,节点仅在响应返回 2xx 状态码时才返回成功。开启此选项后,无论返回什么状态码,都将返回成功。
- 响应格式:选择数据返回的格式。可从以下选项中选择:
- 自动检测(默认):节点会根据返回的数据检测并格式化响应。
- 文件:选择此选项将响应放入文件中。在 将输出放入字段 中输入你希望返回文件的字段名称。
- JSON:选择此选项将响应格式化为 JSON。
- 文本:选择此选项以将响应格式化为纯文本。在 将输出放入字段 中输入要返回文件的字段名称。
分页(Pagination)#
使用此选项可对结果进行分页,这对于处理 API 无法在单次调用中返回的过大查询结果非常有用。
🌐 Use this option to paginate results, useful for handling query results that are too big for the API to return in a single call.
Inspect the API data first
Some options for pagination require knowledge of the data returned by the API you're using. Before setting up pagination, either check the API documentation, or do an API call without pagination, to see the data it returns.
Understand pagination
Pagination means splitting a large set of data into multiple pages. The amount of data on each page depends on the limit you set.
例如,你向名为 /users 的端点发出 API 调用。API 想返回 300 个用户的信息,但一次响应中发送这么多数据太多了。
如果 API 支持分页,你可以逐步获取数据。为此,你可以调用 /users 并设置分页限制,以及页码或 URL 来告诉 API 发送哪一页的数据。在这个例子中,假设你使用的限制是 10,并从第 0 页开始。API 会在响应中发送前 10 个用户。然后,你再次调用 API,将页码增加 1,以获取接下来的 10 个结果。
配置分页设置:
🌐 Configure the pagination settings:
- 分页模式:
- 关闭:关闭分页。
- 在每个请求中更新参数:当你需要为每个请求动态设置参数时使用此方法。
- 响应包含下一个 URL:当 API 响应中包含下一页的 URL 时使用此项。使用一个表达式来设置 下一个 URL。
有关示例设置,请参阅 HTTP 请求节点手册 | 分页。
🌐 For example setups, refer to HTTP Request node cookbook | Pagination.
n8n 提供内置变量,用于在使用分页时处理 HTTP 节点请求和响应:
🌐 n8n provides built-in variables for working with HTTP node requests and responses when using pagination:
| Variable | Description |
|---|---|
$pageCount |
The pagination count. Tracks how many pages the node has fetched. |
$request |
The request object sent by the HTTP node. |
$response |
The response object from the HTTP call. Includes $response.body, $response.headers, and $response.statusCode. The contents of body and headers depend on the data sent by the API. |
API differences
Different APIs implement pagination in different ways. Check the API documentation for the API you're using for details. You need to find out things like:
- Does the API provide the URL for the next page?
- Are there API-specific limits on page size or page number?
- The structure of the data that the API returns.
代理(Proxy)#
如果需要指定 HTTP 代理,请使用此选项。
🌐 Use this option if you need to specify an HTTP proxy.
输入请求应使用的 代理。这优先于通过 HTTP_PROXY、HTTPS_PROXY 或 ALL_PROXY 环境变量 定义的全局设置。
🌐 Enter the Proxy the request should use. This takes precedence over global settings defined with the HTTP_PROXY, HTTPS_PROXY, or ALL_PROXY environment variables.
超时(Timeout)#
使用此选项设置节点等待服务器发送响应头(并开始响应正文)的时间长度。节点会中止超过此初始响应时间的请求。
🌐 Use this option to set how long the node should wait for the server to send response headers (and start the response body). The node aborts requests that exceed this value for the initial response.
输入要等待的超时时间(毫秒)。
🌐 Enter the Timeout time to wait in milliseconds.
仅限工具选项(Tool-only options)#
🌐 The following options are only available when attached to an AI agent as a tool.
优化响应(Optimize Response)#
是否优化工具响应以减少传递给大型语言模型(LLM)的数据量。优化响应可以降低成本,并帮助 LLM 忽略不重要的细节,通常会产生更好的结果。
🌐 Whether to optimize the tool response to reduce the amount of data passed to the LLM. Optimizing the response can reduce costs and can help the LLM ignore unimportant details, often leading to better results.
在优化响应时,你需要选择预期的响应类型,这将决定你可以配置的其他选项。支持的响应类型有:
🌐 When optimizing responses, you select an expected response type, which determines other options you can configure. The supported response types are:
JSON#
在期望收到 JSON 响应时,你可以通过以下选项配置要作为响应使用的 JSON 数据部分:
🌐 When expecting a JSON response, you can configure which parts of the JSON data to use as a response with the following choices:
- 包含数据的字段:此字段用于标识 JSON 对象中包含相关数据的特定部分。你可以留空以使用整个响应。
- 包括字段:这是你选择在响应对象中想要哪些字段的方法。有三种选择:
- 全部:在响应对象中包含所有字段。
- 已选择:仅包含以下指定字段。
- 字段:用逗号分隔的字段列表,用于包含在响应中。你可以使用点符号来指定嵌套字段。你可以从输入面板拖动字段以将其添加到字段列表中。
- 排除:包含所有字段,除了下面指定的字段。
- 字段:用逗号分隔的要从响应中排除的字段列表。你可以使用点表示法指定嵌套字段。你也可以从输入面板拖动字段以将其添加到字段列表中。
超文本标记语言(HTML)#
在预期 HTML 时,你可以识别与 LLM 相关的 HTML 文档部分,并使用以下选项优化响应:
🌐 When expecting HTML, you can identify the part of an HTML document relevant to the LLM and optimize the response with the following options:
- 选择器(CSS):在响应 HTML 中包含的特定元素或元素类型。默认使用
body元素。 - 仅返回内容:是否从响应中去除 HTML 标签和属性,仅保留实际内容。这会使用更少的 token,并且可能更容易让模型理解。
- 要省略的元素:从提取内容时要排除的 CSS 选择器的逗号分隔列表。
- 截断响应:是否限制响应大小以节省令牌。
- 最大响应字符数:HTML 响应中包含的最大字符数。默认值为 1000。
文本(Text)#
在期望得到通用 文本 回应时,你可以通过以下选项优化结果:
🌐 When expecting a generic Text response, you can optimize the results with the following options:
- 截断响应:是否限制响应大小以节省令牌。
- 最大响应字符数:HTML 响应中包含的最大字符数。默认值为 1000。
导入 curl 命令(Import curl command)#
curl 是一个用于通过 URL 传输数据的命令行工具和库。
你可以使用 curl 调用 REST API。如果你想使用的服务的 API 文档提供了 curl 示例,你可以将它们从文档中复制出来,并粘贴到 n8n 中来配置 HTTP 请求节点。
🌐 You can use curl to call REST APIs. If the API documentation of the service you want to use provides curl examples, you can copy them out of the documentation and into n8n to configure the HTTP Request node.
导入 curl 命令:
🌐 Import a curl command:
- 在 HTTP 请求节点的 参数 选项卡中,选择 导入 cURL。导入 cURL 命令 模态窗口将打开。
- 将你的 curl 命令粘贴到文本框中。
- 选择 导入。n8n 会将请求配置加载到节点字段中。这将覆盖任何现有配置。
模板和示例(Templates and examples)#
常见问题(Common issues)#
有关常见问题或问题及建议的解决方案,请参阅 常见问题。
🌐 For common questions or issues and suggested solutions, refer to Common Issues.