A Go HTTP client library with built-in caching, authentication, and automatic background updates.
// 1. Import the package
import "github.com/samhoque/httpclient"
// 2. Create a client
client := httpclient.NewClient(
"https://api.example.com",
httpclient.WithTimeout(10*time.Second),
httpclient.WithAuth(), // Enable cookie handling
)
// 3. Make requests
// Basic request
resp, err := client.Get(context.Background(), "/users")
// Request with custom headers
resp, err = client.Get(context.Background(), "/users",
httpclient.WithRequestHeader("X-Request-ID", "123"),
)
// POST with data
resp, err = client.Post(context.Background(), "/users", user)// Create client with auth enabled
client := httpclient.NewClient(
"https://api.example.com",
httpclient.WithAuth(),
)
// Login - cookies will be automatically saved
loginData := struct {
Username string `json:"username"`
Password string `json:"password"`
}{
Username: "user@example.com",
Password: "password123",
}
resp, err := client.Post(ctx, "/login", loginData)
// Subsequent requests will automatically include saved cookies
resp, err = client.Get(ctx, "/protected-endpoint")// 1. Create a cached client
client := httpclient.NewCachedClient("https://api.example.com")
defer client.Stop()
// 2. Define your data structure
var users []struct {
ID int `json:"id"`
Name string `json:"name"`
}
// 3. Setup automatic cache updates
err := client.SetupCachedEndpoint(
context.Background(),
httpclient.CacheConfig{
Path: "/users",
CronSpec: "*/15 * * * *", // Every 15 minutes
Expiration: 20 * time.Minute,
},
&users,
)
// 4. Use the cached data
data, err := client.GetCached("/users")go get github.com/samhoque/httpclientclient := httpclient.NewClient(
"https://api.example.com",
httpclient.WithAuth(),
httpclient.WithTimeout(5*time.Second),
)// Add custom headers for specific requests
resp, err := client.Get(ctx, "/users",
httpclient.WithRequestHeader("X-Custom-Header", "value"),
httpclient.WithRequestHeader("X-Request-ID", "123"),
)
// Different headers for another request
resp, err = client.Post(ctx, "/data", data,
httpclient.WithRequestHeader("Authorization", "Bearer token"),
)client := httpclient.NewCachedClient("https://api.example.com")
defer client.Stop()
var prices []PriceData
err := client.SetupCachedEndpoint(
context.Background(),
httpclient.CacheConfig{
Path: "/prices",
CronSpec: "*/5 * * * *", // Update every 5 minutes
Expiration: 10 * time.Minute,
},
&prices,
)- β Automatic JSON encoding/decoding
- β Custom headers and timeouts
- β Context support
- β Clean, fluent API
- β Per-request headers
- β Cookie-based authentication
- β Automatic cookie handling
- β Session persistence
- β Support for cookie-based auth flows
- β Per-request header customization
- β Automatic background updates
- β In-memory caching
- β Configurable update schedules
- β Thread-safe operations
WithTimeout(duration)- Set client timeoutWithHeader(key, value)- Add default headersWithAuth()- Enable cookie handling
WithRequestHeader(key, value)- Add headers to specific requests
CacheConfig{
CronSpec: "*/15 * * * *" // Every 15 minutes
CronSpec: "0 * * * *" // Every hour
CronSpec: "0 0 * * *" // Every day at midnight
}// Basic error handling
resp, err := client.Get(ctx, "/users")
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
// Handle timeout
}
return err
}
defer resp.Body.Close()
// Cache error handling
data, err := client.GetCached("/users")
if err != nil {
// If cache expired, fetch fresh data
data, err = client.GetCachedOrFetch(ctx, "/users")
}Authentication with Custom Headers
client := httpclient.NewClient(
"https://api.example.com",
httpclient.WithAuth(),
)
// Login with custom headers
resp, err := client.Post(ctx, "/login", loginData,
httpclient.WithRequestHeader("X-Device-ID", "device123"),
)
// Subsequent authenticated requests
resp, err = client.Get(ctx, "/profile",
httpclient.WithRequestHeader("X-Request-ID", "req123"),
)Custom Configuration
client := httpclient.NewClient(
"https://api.example.com",
httpclient.WithAuth(),
httpclient.WithTimeout(5*time.Second),
httpclient.WithHeader("X-API-Key", "key"),
)- Always
defer client.Stop()for cached clients - Always
defer resp.Body.Close()for responses - Cache expiration is separate from update schedule
- Cookie handling requires
WithAuth()option
- Report issues: GitHub Issues
- Contribute: GitHub Repository
For complete API documentation, see our GoDoc.