Skip to content

상수와 타입

BodyKind

go
type BodyKind int

요청 본문 타입으로, WithBody에서 요청 본문 형식을 지정하는 데 사용합니다.

상수설명Content-Type
BodyAuto0자동 감지타입에 따라 추론
BodyJSON1JSON 강제application/json
BodyXML2XML 강제application/xml
BodyForm3application/x-www-form-urlencoded
BodyBinary4바이너리application/octet-stream
BodyMultipart5멀티파트multipart/form-data

BodyAuto 감지 규칙

입력 타입Content-Type
stringtext/plain; charset=utf-8
[]byteapplication/octet-stream
*FormDatamultipart/form-data
io.Reader설정하지 않음
map[string]stringapplication/x-www-form-urlencoded
기타 타입application/json
go
// 자동 감지 (기본값)
result, _ := client.Post(url, httpc.WithBody(data))

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

// 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 타입, 예: "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))

감사 이벤트

AuditEvent

go
type AuditEvent struct {
    Timestamp     time.Time           `json:"timestamp"`
    Method        string              `json:"method"`
    URL           string              `json:"url"`           // 마스킹됨 (자격 증명 제거)
    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" 또는 "json"
    IncludeHeaders bool     // 요청/응답 헤더 포함
    MaskHeaders    []string // 마스킹할 헤더 이름
    SanitizeError  bool     // 오류 정보 마스킹
}

컨텍스트 키

상수타입설명
SourceIPKeyauditContextKey감사 이벤트의 출처 IP
UserIDKeyauditContextKey감사 이벤트의 사용자 ID
go
// context 로 감사 정보 전달
ctx := context.WithValue(context.Background(), httpc.SourceIPKey, "192.168.1.1")
ctx = context.WithValue(ctx, httpc.UserIDKey, "user-123")

// 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)

// 요청 시 context 의 값이 미들웨어에서 읽힘
result, err := client.Request(ctx, "GET", url)

관련 항목