httpc

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2025 License: MIT Imports: 14 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

# HTTPC - Modern HTTP Client for Go

Go Version License Security Performance Thread Safe

An elegant, high-performance HTTP client library for Go, engineered for production-grade applications. Features enterprise-level security, intelligent concurrency control with goroutine-safe operations, zero-allocation buffer pooling, and adaptive connection management. Designed to handle thousands of concurrent requests while maintaining memory efficiency and thread safety across all operations.

📖 中文文档 - User guide


✨ Why HTTPC?

  • 🛡️ Secure by Default - TLS 1.2+, input validation, CRLF protection, SSRF prevention
  • ⚡ High Performance - Goroutine-safe operations, zero-allocation buffer pooling (90% less GC pressure), intelligent connection reuse
  • 🚀 Massive Concurrency - Handle concurrent requests with adaptive semaphore control and per-host connection limits
  • 🔒 Thread-Safe - All operations are goroutine-safe with lock-free atomic counters and synchronized state management
  • 🔄 Built-in Resilience - Circuit breaker, intelligent retry with exponential backoff, graceful degradation
  • 🎯 Developer Friendly - Simple API, rich options, comprehensive error handling
  • 📊 Observable - Real-time metrics, structured logging, health checks
  • 🔧 Zero Config - Secure defaults, works out of the box

📋 Quick Reference


📑 Table of Contents

🚀 Quick Start

Installation

go get -u github.com/cybergodev/httpc

5-Minute Tutorial

package main

import (
    "fmt"
    "log"
    "github.com/cybergodev/httpc"
)

func main() {
    // Make a simple GET request
    resp, err := httpc.Get("https://api.example.com/users")
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Status: %d\n", resp.StatusCode)
    fmt.Printf("Body: %s\n", resp.Body)

    // POST JSON data
    user := map[string]string{
        "name":  "John Doe",
        "email": "john@example.com",
    }

    // Make a simple POST request
    resp, err = httpc.Post("https://api.example.com/users",
        httpc.WithJSON(user),
        httpc.WithBearerToken("your-token"),
    )
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Status: %d\n", resp.StatusCode)
    fmt.Printf("Body: %s\n", resp.Body)
}

🌐 HTTP Request Methods

💡 Core Concepts: Request Methods vs Option Methods

Before using HTTPC, it's important to understand these two concepts:

🎯 Request Methods ⚙️ Option Methods

Purpose: Specify the HTTP request type ("what to do")

Characteristics:

  • Methods of the Client object
  • Determine the HTTP verb
  • First parameter is the URL

Examples:

client.Get(url, ...)
client.Post(url, ...)
client.Put(url, ...)
client.Delete(url, ...)

Purpose: Customize request parameters ("how to do it")

Characteristics:

  • Functions starting with With
  • Used to configure request details
  • Passed as variadic parameters

Examples:

httpc.WithJSON(data)
httpc.WithQuery("key", "val")
httpc.WithBearerToken("token")
httpc.WithTimeout(30*time.Second)

📝 Usage Pattern

// Basic syntax
resp, err := httpc.RequestMethod(url, option1, option2, ...)

// Real example: POST request + JSON data + auth + timeout
resp, err := httpc.Post("https://api.example.com/users",   // ← Request method
    httpc.WithJSON(userData),                              // ← Option method
    httpc.WithBearerToken("token"),                        // ← Option method
    httpc.WithTimeout(30*time.Second),                     // ← Option method
)

📋 Request Methods Quick Reference

Request Method HTTP Verb Purpose Common Use Cases
Get(url, opts...) GET Retrieve resource Query lists, get details
Post(url, opts...) POST Create resource Submit forms, create records
Put(url, opts...) PUT Full update Replace entire resource
Patch(url, opts...) PATCH Partial update Update specific fields
Delete(url, opts...) DELETE Delete resource Remove records
Head(url, opts...) HEAD Get headers only Check resource existence
Options(url, opts...) OPTIONS Get supported methods CORS preflight
Request(ctx, method, url, opts...) Custom Custom method Special requirements

GET - Retrieve Resource

Purpose: Fetch data from the server without modifying server state.

// 1. Simplest GET request (no options)
resp, err := httpc.Get("https://api.example.com/users")

// 2. With query parameters (using WithQuery option)
resp, err := httpc.Get("https://api.example.com/users",
    httpc.WithQuery("page", 1),        // ← Option: add ?page=1
    httpc.WithQuery("limit", 10),      // ← Option: add &limit=10
)
// Actual request: GET /users?page=1&limit=10

// 3. With authentication and headers (using WithBearerToken and WithHeader options)
resp, err := httpc.Get("https://api.example.com/users",
    httpc.WithBearerToken("your-token"),            // ← Option: add authentication
    httpc.WithHeader("Accept", "application/json"), // ← Option: set header
)

POST - Create Resource

Purpose: Submit data to the server, typically to create a new resource.

// 1. POST JSON data (using WithJSON option)
user := map[string]interface{}{
    "name":  "John Doe",
    "email": "john@example.com",
}
resp, err := httpc.Post("https://api.example.com/users",
    httpc.WithJSON(user),  // ← Option: set JSON request body
)

// 2. POST form data (using WithForm option)
resp, err := httpc.Post("https://api.example.com/login",
    httpc.WithForm(map[string]string{  // ← Option: set form data
        "username": "john",
        "password": "secret",
    }),
)

// 3. POST file upload (using WithFile option)
resp, err := httpc.Post("https://api.example.com/upload",
    httpc.WithFile("file", "document.pdf", fileContent),  // ← Option: upload file
    httpc.WithBearerToken("your-token"),                  // ← Option: add authentication
)

PUT - Full Resource Update

Purpose: Completely replace a resource on the server.

// PUT update entire user object (using WithJSON and WithBearerToken options)
updatedUser := map[string]interface{}{
    "name":  "John Smith",
    "email": "john.smith@example.com",
    "age":   30,
}
resp, err := httpc.Put("https://api.example.com/users/123",
    httpc.WithJSON(updatedUser),         // ← Option: set JSON data
    httpc.WithBearerToken("your-token"), // ← Option: add authentication
)

PATCH - Partial Resource Update

Purpose: Update only specific fields of a resource.

// PATCH update only email field (using WithJSON option)
updates := map[string]interface{}{
    "email": "newemail@example.com",
}
resp, err := httpc.Patch("https://api.example.com/users/123",
    httpc.WithJSON(updates),             // ← Option: set fields to update
    httpc.WithBearerToken("your-token"), // ← Option: add authentication
)

DELETE - Delete Resource

Purpose: Remove a resource from the server.

// 1. Delete specific resource (using WithBearerToken option)
resp, err := httpc.Delete("https://api.example.com/users/123",
    httpc.WithBearerToken("your-token"),  // ← Option: add authentication
)

// 2. Delete with query parameters (using WithQuery option)
resp, err := httpc.Delete("https://api.example.com/cache",
    httpc.WithQuery("key", "session-123"),  // ← Option: specify key to delete
    httpc.WithBearerToken("your-token"),    // ← Option: add authentication
)

HEAD - Get Headers Only

Purpose: Check if a resource exists without fetching the response body.

// Check if resource exists (usually no options needed)
resp, err := httpc.Head("https://api.example.com/users/123")
if err == nil && resp.StatusCode == 200 {
    fmt.Println("Resource exists")
    fmt.Printf("Content length: %d\n", resp.ContentLength)
}

OPTIONS - Get Supported Methods

Purpose: Query the HTTP methods supported by the server.

// Query supported methods for API endpoint (usually no options needed)
resp, err := httpc.Options("https://api.example.com/users")
allowedMethods := resp.Headers.Get("Allow")
fmt.Println("Supported methods:", allowedMethods)  // e.g.: GET, POST, PUT, DELETE

Request - Generic Request Method

Purpose: Send requests with custom HTTP methods.

// Use custom HTTP method (with option methods)
ctx := context.Background()
resp, err := httpc.Request(ctx, "CUSTOM", "https://api.example.com/resource",
    httpc.WithJSON(data),                // ← Option: set data
    httpc.WithHeader("X-Custom", "val"), // ← Option: custom header
)

⚙️ Request Options Explained

Option methods are used to customize various aspects of requests. All option methods start with With and can be combined.

📋 Option Methods Category Quick Reference

Category Purpose Number of Options
Headers Set HTTP request headers 7
Authentication Add authentication info 2
Query Parameters Add URL query parameters 2
Request Body Set request body content 7
File Upload Upload files 1
Timeout & Retry Control timeout and retry 3
Cookies Manage cookies 3

1️⃣ Header Options

Used to set HTTP request headers.

Complete Option List:

  • WithHeader(key, value) - Set single header
  • WithHeaderMap(headers) - Set multiple headers
  • WithUserAgent(ua) - Set User-Agent
  • WithContentType(ct) - Set Content-Type
  • WithAccept(accept) - Set Accept
  • WithJSONAccept() - Set Accept to application/json
  • WithXMLAccept() - Set Accept to application/xml
// Set single header
httpc.Get(url,
    httpc.WithHeader("X-Custom-Header", "value"),
)

// Set multiple headers
httpc.Get(url,
    httpc.WithHeaderMap(map[string]string{
        "X-API-Version": "v1",
        "X-Client-ID":   "client-123",
    }),
)

// Convenience methods for common headers
httpc.Get(url,
    httpc.WithUserAgent("MyApp/1.0"),              // User-Agent
    httpc.WithContentType("application/json"),     // Content-Type
    httpc.WithAccept("application/json"),          // Accept
    httpc.WithJSONAccept(),                        // Accept: application/json
    httpc.WithXMLAccept(),                         // Accept: application/xml
)

2️⃣ Authentication Options

Used to add authentication information.

Complete Option List:

  • WithBearerToken(token) - Bearer Token authentication
  • WithBasicAuth(username, password) - Basic authentication
// Bearer Token authentication (JWT)
httpc.Get(url,
    httpc.WithBearerToken("your-jwt-token"),
)

// Basic authentication
httpc.Get(url,
    httpc.WithBasicAuth("username", "password"),
)

// API Key authentication (using WithHeader)
httpc.Get(url,
    httpc.WithHeader("X-API-Key", "your-api-key"),
)

3️⃣ Query Parameter Options

Used to add URL query parameters (?key=value&...).

Complete Option List:

  • WithQuery(key, value) - Add single query parameter
  • WithQueryMap(params) - Add multiple query parameters
// Add single query parameter
httpc.Get(url,
    httpc.WithQuery("page", 1),
    httpc.WithQuery("filter", "active"),
)
// Result: GET /api?page=1&filter=active

// Add multiple query parameters
httpc.Get(url,
    httpc.WithQueryMap(map[string]interface{}{
        "page":   1,
        "limit":  20,
        "sort":   "created_at",
        "order":  "desc",
    }),
)
// Result: GET /api?page=1&limit=20&sort=created_at&order=desc

4️⃣ Request Body Options

Used to set request body content, supporting multiple formats.

Complete Option List:

  • WithJSON(data) - JSON format request body
  • WithXML(data) - XML format request body
  • WithForm(data) - Form format request body
  • WithText(text) - Plain text request body
  • WithBody(data) - Raw request body
  • WithFormData(formData) - Multipart form data
  • WithBinary(data, contentType) - Binary data
// JSON format (most common)
httpc.Post(url,
    httpc.WithJSON(map[string]interface{}{
        "name": "John",
        "age":  30,
    }),
)
// Content-Type: application/json

// XML format
httpc.Post(url,
    httpc.WithXML(struct {
        Name string `xml:"name"`
        Age  int    `xml:"age"`
    }{Name: "John", Age: 30}),
)
// Content-Type: application/xml

// Form format (application/x-www-form-urlencoded)
httpc.Post(url,
    httpc.WithForm(map[string]string{
        "username": "john",
        "password": "secret",
    }),
)
// Content-Type: application/x-www-form-urlencoded

// Plain text
httpc.Post(url,
    httpc.WithText("Hello, World!"),
)
// Content-Type: text/plain

// Binary data
httpc.Post(url,
    httpc.WithBinary([]byte{0x89, 0x50, 0x4E, 0x47}, "image/png"),
)
// Content-Type: image/png

// Raw data + custom Content-Type
httpc.Post(url,
    httpc.WithBody(customData),
    httpc.WithContentType("application/vnd.api+json"),
)

// Multipart form data (for file uploads)
httpc.Post(url,
    httpc.WithFormData(formData),
)
// Content-Type: multipart/form-data

5️⃣ File Upload Options

Used to upload files to the server.

Complete Option List:

  • WithFile(fieldName, filename, content) - Upload single file (convenience method)
// Simple single file upload
httpc.Post(url,
    httpc.WithFile("file", "document.pdf", fileContent),
)

// Multiple files + form fields (using WithFormData from Request Body Options)
formData := &httpc.FormData{
    Fields: map[string]string{
        "title":       "My Document",
        "description": "Important file",
        "category":    "reports",
    },
    Files: map[string]*httpc.FileData{
        "document": {
            Filename:    "report.pdf",
            Content:     pdfContent,
            ContentType: "application/pdf",
        },
        "thumbnail": {
            Filename:    "preview.jpg",
            Content:     jpgContent,
            ContentType: "image/jpeg",
        },
    },
}
httpc.Post(url,
    httpc.WithFormData(formData),
    httpc.WithBearerToken("token"),  // Can combine with other options
)

6️⃣ Timeout and Retry Options

Used to control request timeout and retry behavior.

Complete Option List:

  • WithTimeout(duration) - Set request timeout
  • WithMaxRetries(n) - Set maximum retries
  • WithContext(ctx) - Use Context for control
// Set request timeout
httpc.Get(url,
    httpc.WithTimeout(30 * time.Second),
)

// Set maximum retries
httpc.Get(url,
    httpc.WithMaxRetries(3),
)

// Use Context to control timeout and cancellation
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
httpc.Get(url,
    httpc.WithContext(ctx),
)

// Combined usage
httpc.Post(url,
    httpc.WithJSON(data),
    httpc.WithTimeout(30 * time.Second),
    httpc.WithMaxRetries(2),
)

Used to add cookies to requests.

Complete Option List:

  • WithCookie(cookie) - Add full cookie
  • WithCookies(cookies) - Add multiple cookies
  • WithCookieValue(name, value) - Add simple cookie
// Simple cookie (name and value only)
httpc.Get(url,
    httpc.WithCookieValue("session_id", "abc123"),
)

// Full cookie (with attributes)
httpc.Get(url,
    httpc.WithCookie(&http.Cookie{
        Name:     "session",
        Value:    "xyz789",
        Path:     "/",
        Domain:   "example.com",
        Secure:   true,
        HttpOnly: true,
    }),
)

// Multiple cookies
httpc.Get(url,
    httpc.WithCookies([]*http.Cookie{
        {Name: "cookie1", Value: "value1"},
        {Name: "cookie2", Value: "value2"},
    }),
)

💡 Option Methods Combination Examples

Option methods can be freely combined to meet various complex requirements:

// Example 1: Complete API request
resp, err := httpc.Post("https://api.example.com/users",
    // Request body
    httpc.WithJSON(userData),
    // Authentication
    httpc.WithBearerToken("your-token"),
    // Headers
    httpc.WithHeader("X-Request-ID", "req-123"),
    httpc.WithUserAgent("MyApp/1.0"),
    // Timeout and retry
    httpc.WithTimeout(30*time.Second),
    httpc.WithMaxRetries(2),
)

// Example 2: File upload + authentication + timeout
resp, err := httpc.Post("https://api.example.com/upload",
    httpc.WithFile("file", "report.pdf", fileContent),
    httpc.WithBearerToken("token"),
    httpc.WithTimeout(60*time.Second),
)

// Example 3: Query + authentication + custom headers
resp, err := httpc.Get("https://api.example.com/users",
    httpc.WithQuery("page", 1),
    httpc.WithQuery("limit", 20),
    httpc.WithBearerToken("token"),
    httpc.WithHeader("X-API-Version", "v2"),
)

📦 Response Handling

The Response object provides convenient methods for handling HTTP responses.

Response Structure

type Response struct {
    StatusCode    int            // HTTP status code
    Status        string         // HTTP status text
    Headers       http.Header    // Response headers
    Body          string         // Response body as string
    RawBody       []byte         // Response body as bytes
    ContentLength int64          // Content length
    Proto         string         // HTTP protocol version
    Duration      time.Duration  // Request duration
    Attempts      int            // Number of retry attempts
    Cookies       []*http.Cookie // Response cookies
}

Status Checking

resp, err := client.Get(url)

// Check success (2xx)
if resp.IsSuccess() {
    fmt.Println("Request successful")
}

// Check redirect (3xx)
if resp.IsRedirect() {
    fmt.Println("Redirected")
}

// Check client error (4xx)
if resp.IsClientError() {
    fmt.Println("Client error")
}

// Check server error (5xx)
if resp.IsServerError() {
    fmt.Println("Server error")
}

Parsing Response Body

// Parse JSON
var result map[string]interface{}
err := resp.JSON(&result)

// Parse XML
var data XMLStruct
err := resp.XML(&data)

// Access raw body
bodyString := resp.Body
bodyBytes := resp.RawBody

Working with Cookies

// Get specific cookie
cookie := resp.GetCookie("session_id")
if cookie != nil {
    fmt.Println("Session:", cookie.Value)
}

// Check if cookie exists
if resp.HasCookie("auth_token") {
    fmt.Println("Authenticated")
}

// Get all cookies
for _, cookie := range resp.Cookies {
    fmt.Printf("%s: %s\n", cookie.Name, cookie.Value)
}

Response Metadata

// Request duration
fmt.Printf("Request took: %v\n", resp.Duration)

// Number of retry attempts
fmt.Printf("Attempts: %d\n", resp.Attempts)

// Content length
fmt.Printf("Size: %d bytes\n", resp.ContentLength)

// Protocol version
fmt.Printf("Protocol: %s\n", resp.Proto)

📥 File Download

HTTPC provides powerful file download capabilities with progress tracking, resume support, and streaming for large files.

Simple File Download

// Download a file to disk using package-level function
result, err := httpc.DownloadFile(
    "https://example.com/file.zip",
    "downloads/file.zip",
)
if err != nil {
    log.Fatal(err)
}

fmt.Printf("Downloaded: %s\n", httpc.FormatBytes(result.BytesWritten))
fmt.Printf("Speed: %s\n", httpc.FormatSpeed(result.AverageSpeed))

Download with Progress Tracking (Package-Level)

// Configure download options
opts := httpc.DefaultDownloadOptions("downloads/large-file.zip")
opts.Overwrite = true
opts.ProgressCallback = func(downloaded, total int64, speed float64) {
    percentage := float64(downloaded) / float64(total) * 100
    fmt.Printf("\rProgress: %.1f%% - %s",
        percentage,
        httpc.FormatSpeed(speed),
    )
}

// Download with progress using package-level function
result, err := httpc.DownloadWithOptions(
    "https://example.com/large-file.zip",
    opts,
    httpc.WithTimeout(10*time.Minute),
)

Download with Progress Tracking (Client Instance)

client, _ := httpc.New()
defer client.Close()

// Configure download options
opts := httpc.DefaultDownloadOptions("downloads/large-file.zip")
opts.Overwrite = true
opts.ProgressCallback = func(downloaded, total int64, speed float64) {
    percentage := float64(downloaded) / float64(total) * 100
    fmt.Printf("\rProgress: %.1f%% - %s",
        percentage,
        httpc.FormatSpeed(speed),
    )
}

// Download with progress using client instance
result, err := client.DownloadWithOptions(
    "https://example.com/large-file.zip",
    opts,
    httpc.WithTimeout(10*time.Minute),
)

Resume Interrupted Downloads

// Enable resume for interrupted downloads
opts := httpc.DefaultDownloadOptions("downloads/file.zip")
opts.ResumeDownload = true  // Resume from where it left off
opts.Overwrite = false      // Don't overwrite, append instead

// Works with both package-level function and client instance
result, err := httpc.DownloadWithOptions(url, opts)
if result.Resumed {
    fmt.Println("Download resumed successfully")
}

Save Response to File

// Alternative: Save any response to file
resp, err := client.Get("https://example.com/data.json")
if err != nil {
    log.Fatal(err)
}

// Save response body to file
err = resp.SaveToFile("data.json")

Download Options

opts := &httpc.DownloadOptions{
    FilePath:         "downloads/file.zip",  // Required: destination path
    Overwrite:        true,                  // Overwrite existing files
    ResumeDownload:   false,                 // Resume partial downloads
    CreateDirs:       true,                  // Create parent directories
    BufferSize:       32 * 1024,             // Buffer size (32KB default)
    ProgressInterval: 500 * time.Millisecond, // Progress update frequency
    ProgressCallback: progressFunc,          // Progress callback function
    FileMode:         0644,                  // File permissions
}

result, err := client.DownloadWithOptions(url, opts)

Download with Authentication

// Download protected files
result, err := client.DownloadFile(
    "https://api.example.com/files/protected.zip",
    "downloads/protected.zip",
    httpc.WithBearerToken("your-token"),
    httpc.WithTimeout(5*time.Minute),
)

🔧 Configuration

Default Configuration

// Uses secure defaults
client, err := httpc.New()

Default Settings:

  • Timeout: 60 seconds
  • MaxRetries: 2
  • TLS: 1.2-1.3
  • HTTP/2: Enabled
  • Connection pooling: Enabled
  • Max concurrent requests: 500
  • Max response body size: 50 MB

Security Presets

// Permissive (Development/Testing)
client, err := httpc.New(httpc.TestingConfig())

// Balanced (Production - Default)
client, err := httpc.New(httpc.DefaultConfig())

// Strict (High Security)
client, err := httpc.New(httpc.SecureConfig())

Custom Configuration

config := &httpc.Config{
    // Network settings
    Timeout:               30 * time.Second,
    DialTimeout:           10 * time.Second,
    KeepAlive:             30 * time.Second,
    TLSHandshakeTimeout:   10 * time.Second,
    ResponseHeaderTimeout: 20 * time.Second,
    IdleConnTimeout:       60 * time.Second,

    // Connection pooling
    MaxIdleConns:        100,
    MaxIdleConnsPerHost: 10,
    MaxConnsPerHost:     20,

    // Security settings
    MinTLSVersion:         tls.VersionTLS12,
    MaxTLSVersion:         tls.VersionTLS13,
    InsecureSkipVerify:    false,
    MaxResponseBodySize:   50 * 1024 * 1024, // 50 MB
    MaxConcurrentRequests: 500,
    ValidateURL:           true,
    ValidateHeaders:       true,
    AllowPrivateIPs:       false,

    // Retry settings
    MaxRetries:    2,
    RetryDelay:    2 * time.Second,
    MaxRetryDelay: 60 * time.Second,
    BackoffFactor: 2.0,
    Jitter:        true,

    // Headers and features
    UserAgent:       "MyApp/1.0",
    FollowRedirects: true,
    EnableHTTP2:     true,
    EnableCookies:   true,
    Headers: map[string]string{
        "Accept": "application/json",
    },
}

client, err := httpc.New(config)

🚨 Error Handling

Intelligent Error Handling

resp, err := httpc.Get(url)
if err != nil {
    // Check for HTTP errors
    var httpErr *httpc.HTTPError
    if errors.As(err, &httpErr) {
        fmt.Printf("HTTP %d: %s\n", httpErr.StatusCode, httpErr.Status)
        fmt.Printf("URL: %s\n", httpErr.URL)
        fmt.Printf("Method: %s\n", httpErr.Method)
    }

    // Check for circuit breaker
    if strings.Contains(err.Error(), "circuit breaker is open") {
        // Service is down, use fallback
        return fallbackData, nil
    }

    // Check for timeout
    if strings.Contains(err.Error(), "timeout") {
        // Handle timeout
        return nil, fmt.Errorf("request timed out")
    }

    return err
}

// Check response status
if !resp.IsSuccess() {
    return fmt.Errorf("unexpected status: %d", resp.StatusCode)
}

Error Types

  • HTTPError: HTTP error responses (4xx, 5xx)
  • Timeout errors: Request timeout exceeded
  • Circuit breaker errors: Service temporarily unavailable
  • Validation errors: Invalid URL or headers
  • Network errors: Connection failures

🎯 Advanced Features

Circuit Breaker

Automatically prevents cascading failures by temporarily blocking requests to failing services.

// Circuit breaker is enabled by default
// It opens after consecutive failures and closes after recovery
client, err := httpc.New()

resp, err := client.Get(url)
if err != nil && strings.Contains(err.Error(), "circuit breaker is open") {
    // Use fallback or cached data
    return getCachedData()
}

Automatic Retries

Intelligent retry mechanism with exponential backoff and jitter.

// Configure retry behavior
config := httpc.DefaultConfig()
config.MaxRetries = 3
config.RetryDelay = 1 * time.Second
config.MaxRetryDelay = 30 * time.Second
config.BackoffFactor = 2.0
config.Jitter = true

client, err := httpc.New(config)

// Per-request retry override
resp, err := client.Get(url,
    httpc.WithMaxRetries(5),
)

Connection Pooling

Efficient connection reuse for better performance.

config := httpc.DefaultConfig()
config.MaxIdleConns = 100        // Total idle connections
config.MaxIdleConnsPerHost = 10  // Idle connections per host
config.MaxConnsPerHost = 20      // Max connections per host
config.IdleConnTimeout = 90 * time.Second

client, err := httpc.New(config)

Automatic cookie handling with cookie jar support.

// Automatic cookie management (enabled by default)
client, err := httpc.New()

// First request sets cookies
resp1, _ := client.Get("https://example.com/login",
    httpc.WithForm(map[string]string{
        "username": "john",
        "password": "secret",
    }),
)

// Subsequent requests automatically include cookies
resp2, _ := client.Get("https://example.com/profile")

// Custom cookie jar
jar, _ := httpc.NewCookieJar()
config := httpc.DefaultConfig()
config.CookieJar = jar
client, err := httpc.New(config)

Resource Management

New in v1.0.0: Proper resource cleanup for long-running applications.

package main

import (
    "github.com/cybergodev/httpc"
)

func main() {
    // Ensure default client is cleaned up on application shutdown
    defer httpc.CloseDefaultClient()

    // Use package-level functions
    resp, err := httpc.Get("https://api.example.com/data")
    // ...
}

Setting Custom Default Client:

// Create a custom client
config := httpc.DefaultConfig()
config.Timeout = 60 * time.Second
client, err := httpc.New(config)
if err != nil {
    log.Fatal(err)
}

// Set as default client (returns error if previous client fails to close)
if err := httpc.SetDefaultClient(client); err != nil {
    log.Printf("Warning: failed to close previous client: %v", err)
}

// Clean up on shutdown
defer httpc.CloseDefaultClient()

Important Notes:

  • CloseDefaultClient() releases all resources (connections, goroutines, etc.)
  • After closing, the default client will be re-initialized on next use
  • SetDefaultClient() now returns an error (breaking change from previous versions)

Context Support

Full context support for cancellation and deadlines.

// Context with timeout
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

resp, err := client.Request(ctx, "GET", url)

// Context with cancellation
ctx, cancel := context.WithCancel(context.Background())
go func() {
    time.Sleep(5 * time.Second)
    cancel() // Cancel after 5 seconds
}()

resp, err := client.Get(url, httpc.WithContext(ctx))

📊 Performance

Concurrency & Thread Safety

  • Massive Concurrency: Handle concurrent requests with adaptive semaphore-based throttling
  • Goroutine-Safe: All operations use atomic counters and synchronized state management
  • Lock-Free Metrics: Real-time performance tracking without contention
  • Per-Host Limits: Intelligent connection distribution prevents host overload

Memory Optimization

  • Zero-Allocation Pooling: Reusable buffer pools reduce GC pressure by 90%
  • Smart Buffer Sizing: Adaptive buffer allocation based on response patterns
  • Memory Bounds: Configurable limits prevent memory exhaustion
  • Efficient Cleanup: Automatic resource reclamation with sync.Pool

Network Performance

  • Connection Pooling: Intelligent connection reuse with per-host tracking
  • HTTP/2 Multiplexing: Multiple concurrent streams over single connection
  • Keep-Alive Optimization: Persistent connections with configurable timeouts
  • Low Latency: Optimized request/response processing pipeline

Reliability

  • Panic Recovery: Comprehensive error handling prevents crashes
  • Circuit Breaker: Automatic failure detection and recovery
  • Graceful Degradation: Continues operation under partial failures
  • Resource Limits: Prevents resource exhaustion with configurable bounds

📖 Documentation

📚 Complete Documentation

💻 Code Examples


📄 License

This project is licensed under the MIT License - see the LICENSE file for details.


🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

🌟 Star History

If you find this project useful, please consider giving it a star! ⭐


Made with ❤️ by the CyberGoDev team

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloseDefaultClient

func CloseDefaultClient() error

CloseDefaultClient closes the default client and resets it. This should be called when the application is shutting down to prevent resource leaks.

func FormatBytes

func FormatBytes(bytes int64) string

FormatBytes formats bytes in human-readable format (e.g., "1.50 KB", "2.30 MB")

func FormatSpeed

func FormatSpeed(bytesPerSecond float64) string

FormatSpeed formats speed in human-readable format (e.g., "1.50 KB/s", "2.30 MB/s")

func NewCookieJar

func NewCookieJar() (http.CookieJar, error)

func SetDefaultClient

func SetDefaultClient(client Client) error

func ValidateConfig added in v1.1.0

func ValidateConfig(cfg *Config) error

ValidateConfig validates the configuration with reasonable limits

Types

type Client

type Client interface {
	// HTTP methods
	Get(url string, options ...RequestOption) (*Response, error)
	Post(url string, options ...RequestOption) (*Response, error)
	Put(url string, options ...RequestOption) (*Response, error)
	Patch(url string, options ...RequestOption) (*Response, error)
	Delete(url string, options ...RequestOption) (*Response, error)
	Head(url string, options ...RequestOption) (*Response, error)
	Options(url string, options ...RequestOption) (*Response, error)

	// Generic request method
	Request(ctx context.Context, method, url string, options ...RequestOption) (*Response, error)

	// File download
	DownloadFile(url string, filePath string, options ...RequestOption) (*DownloadResult, error)
	DownloadWithOptions(url string, downloadOpts *DownloadOptions, options ...RequestOption) (*DownloadResult, error)

	// Client management
	Close() error
}

Client represents the HTTP client interface

func New

func New(config ...*Config) (Client, error)

func NewMinimal added in v1.1.0

func NewMinimal() (Client, error)

func NewPerformance added in v1.1.0

func NewPerformance() (Client, error)

func NewSecure added in v1.1.0

func NewSecure() (Client, error)

type Config

type Config struct {
	Timeout         time.Duration
	MaxIdleConns    int
	MaxConnsPerHost int
	ProxyURL        string

	TLSConfig           *tls.Config
	MinTLSVersion       uint16
	MaxTLSVersion       uint16
	InsecureSkipVerify  bool
	MaxResponseBodySize int64
	AllowPrivateIPs     bool
	StrictContentLength bool

	MaxRetries    int
	RetryDelay    time.Duration
	BackoffFactor float64

	UserAgent       string
	Headers         map[string]string
	FollowRedirects bool
	EnableHTTP2     bool
	EnableCookies   bool
}

func DefaultConfig

func DefaultConfig() *Config

func MinimalConfig added in v1.1.0

func MinimalConfig() *Config

func PerformanceConfig added in v1.1.0

func PerformanceConfig() *Config

func SecureConfig added in v1.1.0

func SecureConfig() *Config

func TestingConfig added in v1.1.0

func TestingConfig() *Config

type DownloadOptions

type DownloadOptions struct {
	FilePath         string
	ProgressCallback DownloadProgressCallback
	Overwrite        bool
	ResumeDownload   bool
}

func DefaultDownloadOptions

func DefaultDownloadOptions(filePath string) *DownloadOptions

type DownloadProgressCallback

type DownloadProgressCallback func(downloaded, total int64, speed float64)

type DownloadResult

type DownloadResult struct {
	FilePath      string
	BytesWritten  int64
	Duration      time.Duration
	AverageSpeed  float64
	StatusCode    int
	ContentLength int64
	Resumed       bool
}

func DownloadFile

func DownloadFile(url string, filePath string, options ...RequestOption) (*DownloadResult, error)

func DownloadWithOptions

func DownloadWithOptions(url string, downloadOpts *DownloadOptions, options ...RequestOption) (*DownloadResult, error)

type FileData

type FileData struct {
	Filename    string
	Content     []byte
	ContentType string
}

type FormData

type FormData struct {
	Fields map[string]string
	Files  map[string]*FileData
}

type HTTPError

type HTTPError struct {
	StatusCode int
	Status     string
	URL        string
	Method     string
}

func (*HTTPError) Error

func (e *HTTPError) Error() string

type Request

type Request struct {
	Method      string
	URL         string
	Headers     map[string]string
	QueryParams map[string]any
	Body        any
	Timeout     time.Duration
	MaxRetries  int
	Context     context.Context
	Cookies     []*http.Cookie
}

type RequestOption

type RequestOption func(*Request)

func WithAccept

func WithAccept(accept string) RequestOption

func WithBasicAuth

func WithBasicAuth(username, password string) RequestOption

func WithBearerToken

func WithBearerToken(token string) RequestOption

func WithBinary

func WithBinary(data []byte, contentType ...string) RequestOption

func WithBody

func WithBody(body any) RequestOption

func WithContentType

func WithContentType(contentType string) RequestOption

func WithContext

func WithContext(ctx context.Context) RequestOption

func WithCookie

func WithCookie(cookie *http.Cookie) RequestOption

func WithCookieValue

func WithCookieValue(name, value string) RequestOption

func WithCookies

func WithCookies(cookies []*http.Cookie) RequestOption

func WithFile

func WithFile(fieldName, filename string, content []byte) RequestOption

func WithForm

func WithForm(data map[string]string) RequestOption

func WithFormData

func WithFormData(data *FormData) RequestOption

func WithHeader

func WithHeader(key, value string) RequestOption

func WithHeaderMap

func WithHeaderMap(headers map[string]string) RequestOption

func WithJSON

func WithJSON(data any) RequestOption

func WithJSONAccept

func WithJSONAccept() RequestOption

func WithMaxRetries

func WithMaxRetries(maxRetries int) RequestOption

func WithQuery

func WithQuery(key string, value any) RequestOption

func WithQueryMap

func WithQueryMap(params map[string]any) RequestOption

func WithText

func WithText(content string) RequestOption

func WithTimeout

func WithTimeout(timeout time.Duration) RequestOption

func WithUserAgent

func WithUserAgent(userAgent string) RequestOption

func WithXML

func WithXML(data any) RequestOption

func WithXMLAccept

func WithXMLAccept() RequestOption

type Response

type Response struct {
	StatusCode    int
	Status        string
	Headers       http.Header
	Body          string
	RawBody       []byte
	ContentLength int64
	Duration      time.Duration
	Attempts      int
	Cookies       []*http.Cookie
}

Response represents an HTTP response

func Delete

func Delete(url string, options ...RequestOption) (*Response, error)

Delete executes a DELETE request using the default client

func Get

func Get(url string, options ...RequestOption) (*Response, error)

Get executes a GET request using the default client

func Head(url string, options ...RequestOption) (*Response, error)

Head executes a HEAD request using the default client

func Options

func Options(url string, options ...RequestOption) (*Response, error)

Options executes an OPTIONS request using the default client

func Patch

func Patch(url string, options ...RequestOption) (*Response, error)

Patch executes a PATCH request using the default client

func Post

func Post(url string, options ...RequestOption) (*Response, error)

Post executes a POST request using the default client

func Put

func Put(url string, options ...RequestOption) (*Response, error)

Put executes a PUT request using the default client

func (*Response) GetCookie

func (r *Response) GetCookie(name string) *http.Cookie

GetCookie returns the named cookie from the response or nil if not found

func (*Response) HasCookie

func (r *Response) HasCookie(name string) bool

HasCookie returns true if the response contains a cookie with the given name

func (*Response) IsClientError

func (r *Response) IsClientError() bool

IsClientError returns true if the response status code indicates a client error (4xx)

func (*Response) IsRedirect

func (r *Response) IsRedirect() bool

IsRedirect returns true if the response status code indicates a redirect (3xx)

func (*Response) IsServerError

func (r *Response) IsServerError() bool

IsServerError returns true if the response status code indicates a server error (5xx)

func (*Response) IsSuccess

func (r *Response) IsSuccess() bool

IsSuccess returns true if the response status code indicates success (2xx)

func (*Response) JSON

func (r *Response) JSON(v any) error

JSON unmarshals the response body into the provided interface

func (*Response) SaveToFile

func (r *Response) SaveToFile(filePath string) error

Directories

Path Synopsis
examples
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL