n8n的HTTP Request节点是连接外部API的万能钥匙。掌握它,你就能集成几乎任何Web服务!本教程将通过循序渐进的讲解和实操案例,让你从零到精通。
HTTP Request节点 是n8n中最强大的核心节点之一。它允许你通过HTTP请求与任何REST API通信,实现数据的获取、创建、更新和删除操作。
在使用HTTP Request节点前,你需要理解4个关键的HTTP方法:
| 方法 | 用途 | CRUD对应 | 示例 |
|---|---|---|---|
| GET | 读取数据 | Read | GET /users/123 |
| POST | 创建新资源 | Create | POST /users (+body) |
| PUT | 更新整个资源 | Update | PUT /users/123 (+body) |
| DELETE | 删除资源 | Delete | DELETE /users/123 |
💡 记忆技巧:GET读、POST创、PUT更、DELETE删。

以获取公共API数据为例(使用JSONPlaceholder测试API):
| 参数 | 值 | 说明 |
|---|---|---|
| Method | GET | 获取数据 |
| URL | https://jsonplaceholder.typicode.com/posts/1 | API端点 |
| Authentication | None | 公共API不需要认证 |
userId、id、title、body等字段的JSON数据✅ 恭喜! 你已经成功调用了第一个API!
选择合适的HTTP方法:
这是API的完整地址。例如:
https://api.example.com/v1/users动态URL:使用表达式引用前面节点的数据:
https://api.example.com/users/{{ $json.userId }}根据API要求选择认证方式:
如果n8n内置了某个服务的认证,直接使用。
Authorization: Bearer your_api_token_hereAuthorizationBearer YOUR_TOKENAuthorization: Basic base64(username:password)添加URL查询参数来过滤或搜索数据。
示例:获取userId=1的所有帖子
https://jsonplaceholder.typicode.com/posts?userId=1在n8n中配置:
userId1某些API要求特定的请求头。
常见请求头:
Content-Type: application/json
Authorization: Bearer token
User-Agent: n8n用于POST、PUT等需要发送数据的请求。
{
"title": "新帖子",
"body": "这是内容",
"userId": 1
}动态JSON:结合前面节点的数据
{
"title": "{{ $json.title }}",
"body": "{{ $json.body }}"
}选择 Form-Data 格式上传文件。
原因:查询参数或请求体格式不正确
解决:
原因:URL端点无效
解决:
原因:认证失败,权限不足
解决:
原因:触发了API速率限制
解决(2种方法):
现在让我们构建一个完整的实用工作流!这个工作流将获取指定城市的天气信息。
{
"name": "获取天气信息",
"nodes": [
{
"parameters": {},
"id": "00ebe6e8-4bc4-4dd9-9d42-4d8a123456789",
"name": "当点击执行工作流时",
"type": "n8n-nodes-base.manualTrigger",
"typeVersion": 1,
"position": [250, 300]
},
{
"parameters": {
"url": "https://api.weatherapi.com/v1/current.json",
"method": "GET",
"sendQuery": true,
"queryParameters": {
"parameters": [
{
"name": "key",
"value": "YOUR_API_KEY_HERE"
},
{
"name": "q",
"value": "Beijing"
},
{
"name": "aqi",
"value": "yes"
}
]
},
"options": {}
},
"id": "c92a3d4f-8b4e-4e1a-9c5e-6f8g9h0i1j2k",
"name": "获取天气",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [500, 300]
},
{
"parameters": {
"functionCode": "return {\n city: $json.location.name,\n country: $json.location.country,\n temperature: $json.current.temp_c + '°C',\n condition: $json.current.condition.text,\n humidity: $json.current.humidity + '%',\n windSpeed: $json.current.wind_kph + ' km/h'\n};"
},
"id": "d8e9f0a1-b2c3-4d4e-5f6g-7h8i9j0k1l2m",
"name": "格式化天气数据",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [750, 300]
}
],
"connections": {
"当点击执行工作流时": {
"main": [
[
{
"node": "获取天气",
"type": "main",
"index": 0
}
]
]
},
"获取天气": {
"main": [
[
{
"node": "格式化天气数据",
"type": "main",
"index": 0
}
]
]
}
},
"active": false,
"settings": {
"executionOrder": "v1"
}
}YOUR_API_KEY_HERE 替换为你的实际API密钥q 参数中的城市名称{
"city": "Beijing",
"country": "China",
"temperature": "15°C",
"condition": "Partly cloudy",
"humidity": "65%",
"windSpeed": "12 km/h"
}
方法:GET
URL:https://api.example.com/users/123
预期响应:返回用户ID为123的信息方法:POST
URL:https://api.example.com/posts/1/comments
请求体:
{
"text": "很好的文章!",
"author": "张三"
}
预期响应:201 Created,返回新创建评论的ID方法:PUT
URL:https://api.example.com/users/123
请求体:
{
"name": "新名字",
"email": "newemail@example.com",
"phone": "13800000000"
}
预期响应:200 OK,返回更新后的用户信息方法:DELETE
URL:https://api.example.com/comments/456
预期响应:204 No Content 或 200 OK在某些场景下,你需要获取HTTP响应头和状态码(不仅仅是响应体)。
配置步骤:
{
"body": { /* 响应内容 */ },
"headers": { /* 响应头 */ },
"statusCode": 200
}某些API可能返回4xx或5xx错误码,但你想继续工作流执行。
配置步骤:
statusCode 进行条件处理如果你从Postman或API文档中找到了cURL命令,可以直接导入!
步骤:
cURL示例:
curl -X GET "https://api.example.com/data?key=value" \
-H "Authorization: Bearer token123" \
-H "Content-Type: application/json"✅ 做这些:
❌ 避免这些:
HTTP Request节点是n8n的瑞士军刀。通过掌握以下要点,你已经可以处理90%的API集成需求:
[1] 官方文档: https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.httprequest/
[2] n8n系列教程: https://www.undsky.com/blog/?category=n8n%E6%95%99%E7%A8%8B#