5、n8n 中调用 API

即使我们没有某个应用的专用节点或现有操作,您仍可在工作流中使用该应用。通过 HTTP 请求节点,您可以调用任何 API 端点,并将返回数据用于工作流中。

主要应用场景:

  • 连接 n8n 尚未集成的应用与服务
  • 网页抓取

运作原理

该工作流可分为三个分支,每个分支具有特定功能:

1.项目拆分(HTTP 请求 - 获取模拟专辑数据):

2.数据抓取(HTTP 请求-获取维基百科页面及 HTML 提取):

  • 该工作流的另一分支通过向"en.wikipedia.org/wiki/Specia... HTTP 请求来获取随机维基百科页面。

  • HTML 提取节点从获取的维基百科页面中抽取文章标题。

3.分页处理(最终分支负责处理 GitHub API 请求的分页问题):

  • 该工作流使用条件判断(If - 是否完成?)来检查是否还有更多页面需要获取,并相应地递增页码(Set - 增加页码)。
  • 这个过程会重复执行,直到获取所有页面数据,从而实现完整的数据收集。

其他

json 复制代码
{
  "name": "7、 HTTP Request",
  "nodes": [
    {
      "parameters": {},
      "id": "1ccf8e89-1b6c-41a0-969e-f5042476f071",
      "name": "On clicking 'execute'",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        -240,
        -112
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "content": "## HTTP Request\n\n此 workflow 展示了 HTTP request 节点最常见的使用场景,以及如何处理其输出\n\n点击 `Execute Workflow` 按钮,并双击节点以查看输入和输出的 items。\n",
        "height": 200,
        "width": 440
      },
      "id": "cd906a35-97ee-4441-a917-a08dd15b9e3c",
      "name": "Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -784,
        -160
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "content": "## 3. Handle Pagination\n有时你需要多次发起相同的请求以获取所需的所有数据(分页)。\n\n### 分页流程如下:\n1. 遍历输入来源的各个页面(`HTTP Request` 节点,名为 \"Get my Starts\")\n2. 在每次循环结束时增加页码(通过 `set` 节点,名为 \"Increment Page\" 完成)\n3. 当没有更多页面时停止循环(在 `If` 节点,名为 \"Are we Finished?\" 检查)\n",
        "height": 460.847917534361,
        "width": 986.3743856726365
      },
      "id": "bc66822c-982b-43a3-8376-2a45ddde2674",
      "name": "Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        112,
        80
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "values": {
          "number": [
            {
              "name": "page"
            },
            {
              "name": "perpage",
              "value": 15
            }
          ],
          "string": [
            {
              "name": "githubUser",
              "value": "that-one-tom"
            }
          ]
        },
        "options": {}
      },
      "id": "747b21b5-3189-4d0e-8350-de39d6f35ac7",
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "position": [
        176,
        320
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "content": "## 2. Data Scraping\n在此示例中,我们使用 `HTTP request` 节点从 n8n 博客获取标题,然后使用 `HTML extract` 节点进行处理。\n\n",
        "height": 280,
        "width": 680
      },
      "id": "4e238485-e573-45d2-8993-bc09d693e5e2",
      "name": "Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        112,
        -224
      ],
      "typeVersion": 1,
      "disabled": true
    },
    {
      "parameters": {
        "content": "## 1. Split into items\n在此示例中,我们将 `HTTP Request` 节点的 body 拆分为更易于管理的 items。",
        "height": 280,
        "width": 500
      },
      "id": "f1b631b0-8562-4e8c-b27e-319fb503c4c7",
      "name": "Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        112,
        -528
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "fieldToSplitOut": "body",
        "options": {}
      },
      "id": "7265c3a9-391c-4504-9972-776a42914b9f",
      "name": "Item Lists - Create Items from Body",
      "type": "n8n-nodes-base.itemLists",
      "position": [
        352,
        -416
      ],
      "notesInFlow": false,
      "typeVersion": 1,
      "notes": "Create Items from Body"
    },
    {
      "parameters": {
        "sourceData": "binary",
        "extractionValues": {
          "values": [
            {
              "key": "ArticleTitle",
              "cssSelector": "#firstHeading"
            }
          ]
        },
        "options": {}
      },
      "id": "ed096820-cf58-4495-862d-438018e8590b",
      "name": "HTML Extract - Extract Article Title",
      "type": "n8n-nodes-base.htmlExtract",
      "position": [
        336,
        -112
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "fieldToSplitOut": "body",
        "options": {}
      },
      "id": "d54adebf-ba40-4821-a31a-28b4c6500b0a",
      "name": "Item Lists - Fetch Body",
      "type": "n8n-nodes-base.itemLists",
      "position": [
        544,
        320
      ],
      "typeVersion": 1,
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$node[\"HTTP Request - Get my Stars\"].json[\"body\"]}}",
              "operation": "isEmpty"
            }
          ]
        }
      },
      "id": "ee678e31-f8ce-4138-878c-6c4526002141",
      "name": "If - Are we finished?",
      "type": "n8n-nodes-base.if",
      "position": [
        720,
        320
      ],
      "executeOnce": true,
      "typeVersion": 1
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "page",
              "value": "={{$node[\"Set\"].json[\"page\"]++}}"
            }
          ]
        },
        "options": {}
      },
      "id": "8d5c2863-1b3d-4353-93bf-c96d27bc57e1",
      "name": "Set - Increment Page",
      "type": "n8n-nodes-base.set",
      "position": [
        944,
        352
      ],
      "executeOnce": true,
      "typeVersion": 1
    },
    {
      "parameters": {
        "url": "https://jsonplaceholder.typicode.com/albums",
        "options": {
          "response": {
            "response": {
              "fullResponse": true
            }
          }
        }
      },
      "id": "291ac0c1-85c9-490a-9475-279cfb8eb624",
      "name": "HTTP Request - Get Mock Albums",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        176,
        -416
      ],
      "typeVersion": 3
    },
    {
      "parameters": {
        "url": "https://en.wikipedia.org/wiki/Special:Random",
        "options": {
          "redirect": {
            "redirect": {
              "followRedirects": true
            }
          },
          "response": {
            "response": {
              "responseFormat": "file"
            }
          }
        }
      },
      "id": "54c78fbb-70c7-43c7-8a4f-0b4f4cabb1b4",
      "name": "HTTP Request - Get Wikipedia Page",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        160,
        -112
      ],
      "typeVersion": 3
    },
    {
      "parameters": {
        "url": "=https://api.github.com/users/{{$node[\"Set\"].json[\"githubUser\"]}}/starred",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "per_page",
              "value": "={{$node[\"Set\"].json[\"perpage\"]}}"
            },
            {
              "name": "page",
              "value": "={{$node[\"Set\"].json[\"page\"]}}"
            }
          ]
        },
        "options": {
          "response": {
            "response": {
              "fullResponse": true
            }
          }
        }
      },
      "id": "75d99d10-dfd5-4d84-b3ce-d7efe76c522c",
      "name": "HTTP Request - Get my Stars",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        352,
        320
      ],
      "typeVersion": 3
    }
  ],
  "pinData": {},
  "connections": {
    "Set": {
      "main": [
        [
          {
            "node": "HTTP Request - Get my Stars",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set - Increment Page": {
      "main": [
        [
          {
            "node": "HTTP Request - Get my Stars",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If - Are we finished?": {
      "main": [
        [],
        [
          {
            "node": "Set - Increment Page",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "On clicking 'execute'": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          },
          {
            "node": "HTTP Request - Get Mock Albums",
            "type": "main",
            "index": 0
          },
          {
            "node": "HTTP Request - Get Wikipedia Page",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Item Lists - Fetch Body": {
      "main": [
        [
          {
            "node": "If - Are we finished?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request - Get my Stars": {
      "main": [
        [
          {
            "node": "Item Lists - Fetch Body",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request - Get Mock Albums": {
      "main": [
        [
          {
            "node": "Item Lists - Create Items from Body",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request - Get Wikipedia Page": {
      "main": [
        [
          {
            "node": "HTML Extract - Extract Article Title",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "36c9e0e9-a945-4311-b503-24e31922e954",
  "meta": {
    "instanceId": "93bc91f5b94c286bd0f4f65621745c9fee427fafd468de9d56a6b7a434a37fbc"
  },
  "id": "4cBzGMvzTQR4Y4Ri",
  "tags": []
}
相关推荐
新智元2 小时前
世界首富换人!81 岁硅谷狂人 4000 亿身价碾压马斯克,33 岁华裔才女逆袭
人工智能·openai
机器之心2 小时前
交互扩展时代来临:创智复旦字节重磅发布AgentGym-RL,昇腾加持,开创智能体训练新范式
人工智能·openai
新智元4 小时前
刚刚,英伟达祭出下一代 GPU!狂飙百万 token 巨兽,投 1 亿爆赚 50 亿
人工智能·openai
一泽Eze8 小时前
AI 不懂「疯狂星期四」
aigc·openai
飞哥数智坊1 天前
解决AI幻觉,只能死磕模型?OpenAI给出不一样的思路
人工智能·openai
伊玛目的门徒1 天前
告别 OpenAI SDK:如何使用 Python requests 库调用大模型 API(例如百度的ernie-4.5-turbo)
python·openai·requests·大模型调用·ernie-4.5-turbo
安思派Anspire1 天前
你不需要 GraphRAG!构建一个多策略图谱思维(Multi-Strategy Graph Thinking)AI Agent
aigc·openai·agent
新智元1 天前
=COPILOT() 函数横空出世!AI 自动写公式效率起飞,网友:让 Excel 再次伟大
人工智能·openai
新智元1 天前
狂登热搜,iPhone 17「挤爆牙膏」!5999 起价,AirPods 变身同声传译
人工智能·openai
爱学习的茄子2 天前
Function Call:让AI从文本生成走向智能交互的技术革命
前端·深度学习·openai