← 返回
效率工具 中文

SimpleHttpSkill

Make HTTP requests (GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS) with custom headers, automatic retries, and graceful error handling. Use when the user need...
支持自定义标头、自动重试和优雅错误处理的HTTP请求(GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS)。适用于用户需要...
stephen-standridge
效率工具 clawhub v0.1.0 1 版本 100000 Key: 无需
★ 0
Stars
📥 939
下载
💾 8
安装
1
版本
#latest

概述

Simple HTTP Skill

Make HTTP requests using only Node.js built-in modules. Supports all standard

methods, arbitrary headers, automatic retries with exponential backoff, and

never throws on failure — always resolves with an inspectable response object.

Required Inputs

  • url (string): Fully qualified URL to request.
  • method (string, optional): HTTP method. Default GET.
  • headers (object, optional): Request headers.
  • body (string | Buffer | object, optional): Request body. Objects are auto-serialized to JSON.
  • maxRetries (number, optional): Retry attempts for transient failures. Default 3.
  • timeout (number, optional): Socket timeout in ms. Default 30000.

Step-by-Step Workflow

  1. Import the client from src/http-client.js:
const { HttpClient } = require("./src/http-client");
  1. Create a client instance (optionally set default headers shared across calls):
const client = new HttpClient({
  defaultHeaders: { Authorization: "Bearer <token>" },
  maxRetries: 3,
});
  1. Make requests using convenience methods or the generic request():
// GET
const resp = await client.get("https://api.example.com/items");

// POST with JSON body
const resp = await client.post("https://api.example.com/items", {
  body: { name: "widget" },
});

// PUT with custom headers
const resp = await client.put("https://api.example.com/items/1", {
  headers: { "X-Request-Id": "abc123" },
  body: { name: "updated" },
});

// DELETE
const resp = await client.delete("https://api.example.com/items/1");

// Generic form — any method
const resp = await client.request("PATCH", "https://api.example.com/items/1", {
  body: { qty: 5 },
});
  1. Inspect the response:
if (resp.ok) {
  console.log(resp.body);      // parsed JSON or raw string
  console.log(resp.status);    // e.g. 200
  console.log(resp.headers);   // response headers object
} else {
  console.log(resp.error);     // human-readable error (null if HTTP error with status)
  console.log(resp.status);    // HTTP status code or null for network errors
}

Output Format

Every call resolves with an object containing:

KeyTypeDescription
----------------------------------------------------------------------------------
okbooleantrue if status is 2xx
status`number \null`HTTP status code; null for network-level errors
headersobjectResponse headers
bodyanyParsed JSON (if content-type is JSON), else string
error`string \null`Error description on failure; null on success

Error Handling & Retry Behavior

  • Retried automatically: Connection errors, timeouts, and HTTP 429 / 5xx responses.
  • Not retried: 4xx errors (except 429) — returned immediately.
  • Backoff: Exponential with jitter (base 500ms, capped at 30s).
  • Graceful failure: The client never throws. After exhausting retries, it resolves with the last error response so the caller can always inspect resp.ok and resp.error.

Configuration Options

All options can be set at the client level (constructor) and overridden per-request:

OptionDefaultDescription
------------------------------------------------------------------
defaultHeaders{}Headers applied to every request
maxRetries3Max retry attempts
timeout30000Socket timeout in ms
backoffBase500Base delay (ms) for exponential backoff
backoffMax30000Maximum backoff delay cap (ms)

Dependencies

None — uses only Node.js built-in modules (http, https, url).

版本历史

共 1 个版本

  • v0.1.0 当前
    2026-03-29 19:10 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

productivity

Weather

steipete
获取当前天气和预报(无需API密钥)
★ 446 📥 226,408
productivity

Word / DOCX

ivangdavila
创建、检查和编辑 Microsoft Word 文档及 DOCX 文件,支持样式、编号、修订记录、表格、分节符及兼容性检查等功能。
★ 440 📥 147,966
productivity

Obsidian

steipete
操作 Obsidian 仓库(纯 Markdown 笔记)并通过 obsidian-cli 自动化。
★ 432 📥 103,829