N8N中文教程
集成节点/Creating_nodes/Build_your_node/Reference

面向节点开发者的 HTTP 请求辅助工具#

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

仅适用于编程式风格#

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

使用方法#

execute 函数内调用该辅助工具:

// 无需身份验证的情况
const response = await this.helpers.httpRequest(options);

// 需要身份验证的情况
const response = await this.helpers.httpRequestWithAuthentication.call(
  this,
  'credentialTypeName', // 例如: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`。

关于各字段的说明:
* `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]}`),该参数定义 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`:设为 true 时返回包含更多数据的完整响应对象,格式为:`{body: body, headers: object, statusCode: 200, statusMessage: 'OK'}`
* `encoding`:n8n 可自动检测内容类型,但也可指定 `arrayBuffer` 来获取可读取和交互的 Buffer

## 示例[#](https://docs.n8n.io/integrations/creating-nodes/build/reference/http-helpers/#example "永久链接")
有关示例,请参考 [Mattermost 节点](https://github.com/n8n-io/n8n/blob/master/packages/nodes-base/nodes/Mattermost/v1/MattermostV1.node.ts)。

## 旧版辅助函数的弃用[#](https://docs.n8n.io/integrations/creating-nodes/build/reference/http-helpers/#deprecation-of-the-previous-helper "永久链接")
之前使用 `this.helpers.request(options)` 的辅助函数实现基于 `request-promise` 库,该库已在版本 1 中移除。
为最大限度保证兼容性,n8n 已将其透明转换至名为 `Axios` 的库。
如遇问题,请在[社区论坛](https://community.n8n.io/)或[GitHub](https://github.com/n8n-io/n8n/issues)上报告。

## 新版辅助函数迁移指南[#](https://docs.n8n.io/integrations/creating-nodes/build/reference/http-helpers/#migration-guide-to-the-new-helper "永久链接")
新版辅助函数更健壮、不依赖特定库且更易使用。
新建节点均应使用新版辅助函数。强烈建议将现有自定义节点迁移至新版辅助函数。迁移时主要注意事项如下:
  * 接受 `url` 参数,不再接受 `uri`
  * `encoding: null` 需改为 `encoding: arrayBuffer`
  * `rejectUnauthorized: false` 需改为 `skipSslCertificateValidation: true`
  * 根据 `content-type` 请求头规范使用 `body` 参数传递载荷
  * `resolveWithFullResponse` 改为 `returnFullResponse`,功能保持一致

---