节点构建者的 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 | |
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 是必填项,其它字段为可选项。默认方法是 GET。
关于可用字段的一些说明:
🌐 Some notes about the possible fields:
body:您可以使用常规的 JavaScript 对象来处理 JSON 数据,使用 buffer 来上传文件,使用 FormData 实例来处理multipart/form-data,以及URLSearchParams用于application/x-www-form-urlencoded。headers:一个键值对。- 如果
body是FormData的一个实例,那么 n8n 会自动添加content-type: multipart/form-data。 - 如果
body是URLSearchParams的实例,那么 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]=cbrackets:{ a: ['b', 'c'] }作为a[]=b&a[]=crepeat:{ a: ['b', 'c'] }作为a=b&a=ccomma:{ a: ['b', 'c'] }作为a=b,c
auth:用于基本认证。请提供username和password。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.
🌐 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,并且具有类似的行为