Skip to content

Constants and Types

BodyKind

go
type BodyKind int

Request body type, used with WithBody to specify the body format.

ConstantValueDescriptionContent-Type
BodyAuto0Auto-detectInferred from type
BodyJSON1Force JSONapplication/json
BodyXML2Force XMLapplication/xml
BodyForm3Formapplication/x-www-form-urlencoded
BodyBinary4Binaryapplication/octet-stream
BodyMultipart5Multipartmultipart/form-data

BodyAuto Detection Rules

Input TypeContent-Type
stringtext/plain; charset=utf-8
[]byteapplication/octet-stream
*FormDatamultipart/form-data
io.ReaderNot set
map[string]stringapplication/x-www-form-urlencoded
Other typesapplication/json
go
// Auto-detect (default)
result, _ := client.Post(url, httpc.WithBody(data))

// Force JSON
result, _ := client.Post(url, httpc.WithBody(data, httpc.BodyJSON))

// Force XML
result, _ := client.Post(url, httpc.WithBody(data, httpc.BodyXML))

FormData / FileData

FormData

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

FileData

go
type FileData struct {
    Filename    string
    Content     []byte
    ContentType string  // MIME type, e.g. "image/png", "application/pdf"
}
go
form := &httpc.FormData{
    Fields: map[string]string{"key": "value"},
    Files: map[string]*httpc.FileData{
        "file": {Filename: "test.txt", Content: []byte("hello"), ContentType: "text/plain"},
    },
}
result, err := client.Post(url, httpc.WithFormData(form))

Audit Events

AuditEvent

go
type AuditEvent struct {
    Timestamp     time.Time           `json:"timestamp"`
    Method        string              `json:"method"`
    URL           string              `json:"url"`           // Masked (credentials removed)
    StatusCode    int                 `json:"statusCode"`
    Duration      time.Duration       `json:"duration"`
    Attempts      int                 `json:"attempts"`
    Error         error               `json:"error,omitempty"`
    SourceIP      string              `json:"sourceIP,omitempty"`
    UserID        string              `json:"userID,omitempty"`
    RedirectChain []string            `json:"redirectChain,omitempty"`
    ReqHeaders    map[string][]string `json:"reqHeaders,omitempty"`
    RespHeaders   map[string][]string `json:"respHeaders,omitempty"`
}

AuditMiddlewareConfig

go
type AuditMiddlewareConfig struct {
    Format         string   // "text" or "json"
    IncludeHeaders bool     // Include request/response headers
    MaskHeaders    []string // Header names to mask
    SanitizeError  bool     // Mask error messages
}

Context Keys

ConstantTypeDescription
SourceIPKeyauditContextKeySource IP in audit events
UserIDKeyauditContextKeyUser ID in audit events
go
// Pass audit information via context
ctx := context.WithValue(context.Background(), httpc.SourceIPKey, "192.168.1.1")
ctx = context.WithValue(ctx, httpc.UserIDKey, "user-123")

// Configure audit middleware in Config
cfg := httpc.DefaultConfig()
cfg.Middleware.Middlewares = []httpc.MiddlewareFunc{
    httpc.AuditMiddleware(func(event httpc.AuditEvent) {
        fmt.Println(event.SourceIP) // 192.168.1.1
        fmt.Println(event.UserID)   // user-123
    }),
}
client, _ := httpc.New(cfg)

// Values from context are read by the middleware when sending requests
result, err := client.Request(ctx, "GET", url)

See Also