Skip to content

节点构建者的 HTTP 请求助手(HTTP request helper for node builders)#

n8n 提供了一个灵活的 HTTP 请求辅助工具,它抽象化了大部分复杂性。

🌐 n8n provides a flexible helper for making HTTP requests, which abstracts away most of the complexity.

仅限程序化风格

本文件中的信息适用于使用程序化风格构建节点。不适用于声明式风格的节点。

用法(Usage)#

execute 函数内部调用辅助函数。

🌐 Call the helper inside the execute function.

1
2
3
4
5
6
7
8
9
// If no auth needed
const response = await this.helpers.httpRequest(options);

// If auth needed
const response = await this.helpers.httpRequestWithAuthentication.call(
	this, 
	'credentialTypeName', // For example: pipedriveApi
	options,
);

options 是一个对象:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
{
	url: string;
	headers?: object;
	method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD';
	body?: FormData | Array | string | number | object | Buffer | URLSearchParams;
	qs?: object;
	arrayFormat?: 'indices' | 'brackets' | 'repeat' | 'comma';
	auth?: {
		username: string,
		password: string,
	};
	disableFollowRedirect?: boolean;
	encoding?: 'arraybuffer' | 'blob' | 'document' | 'json' | 'text' | 'stream';
	skipSslCertificateValidation?: boolean;
	returnFullResponse?: boolean;
	proxy?: {
		host: string;
		port: string | number;
		auth?: {
			username: string;
			password: string;
		},
		protocol?: string;
	};
	timeout?: number;
	json?: boolean;
}	

url 是必填项,其它字段为可选项。默认方法是 GET

关于可用字段的一些说明:

🌐 Some notes about the possible fields:

  • body:您可以使用常规的 JavaScript 对象来处理 JSON 数据,使用 buffer 来上传文件,使用 FormData 实例来处理 multipart/form-data,以及 URLSearchParams 用于 application/x-www-form-urlencoded
  • headers:一个键值对。
    • 如果 bodyFormData 的一个实例,那么 n8n 会自动添加 content-type: multipart/form-data
    • 如果 bodyURLSearchParams 的实例,那么 n8n 会添加 content-type: application/x-www-form-urlencoded
    • 要覆盖此行为,请设置 content-type 头。
  • arrayFormat:如果你的查询字符串包含一个数据数组,例如 const qs = {IDs: [15,17]}arrayFormat 的值决定了 n8n 如何格式化它。
    • indices(默认):将{ a: ['b', 'c'] }作为a[0]=b&a[1]=c
    • brackets{ a: ['b', 'c'] } 作为 a[]=b&a[]=c
    • repeat{ a: ['b', 'c'] } 作为 a=b&a=c
    • comma{ a: ['b', 'c'] } 作为 a=b,c
  • auth:用于基本认证。请提供 usernamepassword。n8n 建议省略此项,改用 helpers.httpRequestWithAuthentication(...)
  • disableFollowRedirect:默认情况下,n8n 会跟随重定向。您可以将其设置为 true 以防止这种情况发生。
  • skipSslCertificateValidation:用于在没有正确证书的情况下调用 HTTPS 服务
  • returnFullResponse:不是只返回主体,而是以以下格式返回包含更多数据的对象:{body: body, headers: object, statusCode: 200, statusMessage: 'OK'}
  • encoding:n8n 可以检测内容类型,但你可以指定 arrayBuffer 来接收一个可以读取和交互的缓冲区。

示例(Example)#

例如,请参阅 Mattermost 节点

🌐 For an example, refer to the Mattermost node.

已弃用的辅助函数(Deprecation of the previous helper)#

之前使用 this.helpers.request(options) 的辅助实现使用并暴露了 request-promise 库。这在版本 1 中已被移除。

🌐 The previous helper implementation using this.helpers.request(options) used and exposed the request-promise library. This was removed in version 1.

为了将不兼容性降到最低,n8n 对另一个名为 Axios 的库进行了透明转换。

🌐 To minimize incompatibility, n8n made a transparent conversion to another library called Axios.

如果你遇到问题,请在社区论坛GitHub上报告。

🌐 If you are having issues, please report them in the Community Forums or on GitHub.

迁移到新助手的指南(Migration guide to the new helper)#

新的辅助函数更加健壮、与库无关且更易于使用。

🌐 The new helper is much more robust, library agnostic, and easier to use.

新的节点都应该使用新的辅助工具。你应该强烈考虑将现有的自定义节点迁移到新的辅助工具。迁移时的主要注意事项如下:

🌐 New nodes should all use the new helper. You should strongly consider migrating existing custom nodes to the new helper. These are the main considerations when migrating:

  • 接受 url。不接受 uri
  • encoding: null 现在必须是 encoding: arrayBuffer
  • rejectUnauthorized: false 现在是 skipSslCertificateValidation: true
  • 根据 content-type 头使用 body 来明确载荷。
  • resolveWithFullResponse 现在是 returnFullResponse,并且具有类似的行为