httpx(enhanced HTTP client)

A fluent, chainable HTTP client with enhanced functionality for building and executing HTTP requests with automatic error handling and JSON support.

The httpx module provides a fluent, chainable HTTP client with enhanced functionality for building and executing HTTP requests with automatic error handling and JSON support.

Features

  • Fluent API design with method chaining
  • Automatic JSON handling for request/response bodies
  • Custom HTTP client support for advanced configurations
  • Query parameter management with automatic URL encoding
  • Header manipulation with multiple value support
  • Error propagation throughout the chain
  • Context support for request cancellation and timeouts

Basic HTTP Operations

 1package main
 2
 3import (
 4    "context"
 5    "fmt"
 6    "net/http"
 7    "time"
 8
 9    "github.com/crazyfrankie/frx/httpx"
10)
11
12func main() {
13    ctx := context.Background()
14    
15    // Simple GET request
16    resp := httpx.NewRequest(ctx, http.MethodGet, "https://api.example.com/users").
17        AddParam("page", "1").
18        AddParam("limit", "10").
19        AddHeader("Authorization", "Bearer token123").
20        Do()
21    
22    if resp.err != nil {
23        panic(resp.err)
24    }
25    defer resp.Body.Close()
26    
27    fmt.Printf("Status: %d\n", resp.StatusCode)
28}

JSON Request/Response Handling

 1type CreateUserRequest struct {
 2    Name  string `json:"name"`
 3    Email string `json:"email"`
 4}
 5
 6type User struct {
 7    ID    int64  `json:"id"`
 8    Name  string `json:"name"`
 9    Email string `json:"email"`
10}
11
12func CreateUser(ctx context.Context, req CreateUserRequest) (*User, error) {
13    var user User
14    
15    resp := httpx.NewRequest(ctx, http.MethodPost, "https://api.example.com/users").
16        AddHeader("Content-Type", "application/json").
17        AddHeader("Authorization", "Bearer token123").
18        JSONBody(req).  // Automatically marshals to JSON
19        Do()
20    
21    if resp.err != nil {
22        return nil, resp.err
23    }
24    defer resp.Body.Close()
25    
26    // Automatically unmarshal JSON response
27    if err := resp.JSONReceive(&user); err != nil {
28        return nil, err
29    }
30    
31    return &user, nil
32}

Custom HTTP Client Configuration

 1func main() {
 2    // Custom client with timeout and retry logic
 3    customClient := &http.Client{
 4        Timeout: 30 * time.Second,
 5        Transport: &http.Transport{
 6            MaxIdleConns:        100,
 7            MaxIdleConnsPerHost: 10,
 8            IdleConnTimeout:     90 * time.Second,
 9        },
10    }
11    
12    resp := httpx.NewRequest(ctx, http.MethodGet, "https://api.example.com/data").
13        Client(customClient).  // Use custom client
14        AddParam("format", "json").
15        AddHeader("User-Agent", "MyApp/1.0").
16        Do()
17    
18    // Handle response...
19}

Error Handling and Chaining

 1func FetchUserData(ctx context.Context, userID string) (*User, error) {
 2    var user User
 3    
 4    resp := httpx.NewRequest(ctx, http.MethodGet, "https://api.example.com/users/"+userID).
 5        AddHeader("Authorization", "Bearer token123").
 6        AddParam("include", "profile,settings").
 7        Do()
 8    
 9    // Error is propagated through the chain
10    if resp.err != nil {
11        return nil, fmt.Errorf("failed to fetch user: %w", resp.err)
12    }
13    defer resp.Body.Close()
14    
15    if resp.StatusCode != http.StatusOK {
16        return nil, fmt.Errorf("API returned status %d", resp.StatusCode)
17    }
18    
19    if err := resp.JSONReceive(&user); err != nil {
20        return nil, fmt.Errorf("failed to decode response: %w", err)
21    }
22    
23    return &user, nil
24}