n8n 中文文档

HTTP 节点构建器的请求帮助器

n8n 提供了一个灵活的帮助程序来发出 HTTP 请求,从而消除了大部分复杂性。

提示

仅限程序化风格

本文档中的信息适用于使用编程风格构建节点。它不适用于声明式样式节点。

用途

在 execute 函数内调用帮助程序。

// 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 是一个对象:

{
    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。

关于可能字段的一些注释:

  • body:您可以将常规 JavaScript 对象用于 JSON 有效负载、文件上传缓冲区、multipart/form-data 的 FormData 实例以及 application/x-www-form-urlencoded 的 URLSearchParams。
  • 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]=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:用于基本身份验证。提供username和password。 n8n 建议忽略此项,并使用 helpers.httpRequestWithAuthentication(...) 代替。
  • disableFollowRedirect:默认情况下,n8n 遵循重定向。您可以将其设置为 true 以防止这种情况发生。
  • skipSslCertificateValidation:用于在没有适当证书的情况下调用HTTPS服务
  • returnFullResponse:不只返回主体,而是返回具有以下格式的更多数据的对象:{body: body, headers: object, statusCode: 200, statusMessage: 'OK'}
  • encoding:n8n 可以检测内容类型,但您可以指定 arrayBuffer 来接收可以读取并与之交互的缓冲区。

示例

有关示例,请参阅 最重要的节点。

弃用以前的帮助程序

警告

功能可用性

之前的 this.helpers.request(options) 实现使用并公开了 request-promise 库,已从 n8n 1.0 中删除。

为了最大限度地减少不兼容性,n8n 透明地转换为另一个名为 Axios 的库。

如果您遇到问题,请在 社区论坛 或 GitHub 中报告。

新助手 的迁移指南

新的助手更加强大,与库无关,并且更易于使用。

新节点都应该使用新的助手。您应该强烈考虑将现有的自定义节点迁移到新的帮助器。以下是迁移时的主要考虑因素:

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

相关资源

不想自己部署和维护 n8n?n8ncn 托管 n8n 云服务:开通账号即可使用,不用买服务器;也可以在 n8ncn 工作流库 找现成的中文工作流。