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]=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来接收可以读取并与之交互的缓冲区。
示例
有关示例,请参阅 最重要的节点。
弃用以前的帮助程序
警告
功能可用性
之前的 this.helpers.request(options) 实现使用并公开了 request-promise 库,已从 n8n 1.0 中删除。
为了最大限度地减少不兼容性,n8n 透明地转换为另一个名为 Axios 的库。
新助手 的迁移指南
新的助手更加强大,与库无关,并且更易于使用。
新节点都应该使用新的助手。您应该强烈考虑将现有的自定义节点迁移到新的帮助器。以下是迁移时的主要考虑因素:
- 接受
url。不接受uri。 encoding: null现在必须是encoding: arrayBuffer。rejectUnauthorized: false现在是skipSslCertificateValidation: true- 根据
content-type标头使用body来澄清有效负载。 resolveWithFullResponse现在是returnFullResponse并且具有类似的行为
相关资源
不想自己部署和维护 n8n?n8ncn 托管 n8n 云服务:开通账号即可使用,不用买服务器;也可以在 n8ncn 工作流库 找现成的中文工作流。