hc

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2026 License: MIT Imports: 11 Imported by: 0

README

HC (Http Client)

test

Simple HTTP Client for go with interceptor support.

import "github.com/morkid/hc"

helloWorld := hc.Interceptor{
    TakeOver: func(req *http.Request) (res *http.Response, err error) {
        return &http.Response{
            Body:       io.NopCloser(strings.NewReader(`{"message":"hello world"}`)),
            Status:     "200 OK",
            StatusCode: 200,
            Proto:      "HTTP/1.1",
        }, nil
    },
}

client := hc.New(hc.Config{
    LogEnabled: true,
    Interceptor: func (req *http.Request) error {
        // customize http header
        req.Header.Add("Accept", "application/json")

        if req.URL.Path == "/hello-world.json" {
            // mock response
            return &helloWorld
        }

        // forward to the original request
        return nil
    }
})

req, _ := http.NewRequest("GET", "http://example.com/hello-world.json", nil)
res, err := client.Do(req)
log.Println(err)
log.Println(res.StatusCode)

Resty integration:


client := hc.New()

restyClient := resty.New()

restyClient.SetTransport(client.Transport)

res, err := restyClient.R().Get("http://example.com/hello-world.json")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JsonResponse

func JsonResponse(res *http.Response, obj any) error

func New

func New(configs ...Config) *http.Client

New create new http client

Types

type Config

type Config struct {
	LogEnabled             bool                          // Enable log
	LogResponseBodyEnabled bool                          // Enable log for response body
	LogHeaderEnabled       bool                          // Enable header logging
	LogPrefix              string                        // Log Prefix
	Logger                 *log.Logger                   // Logger instance
	Interceptor            func(req *http.Request) error // Intercept request
	Timeout                int                           // Timeout seconds
}

type Interceptor

type Interceptor struct {
	ErrorMessage string
	TakeOver     func(req *http.Request) (res *http.Response, err error)
}

func (*Interceptor) Error

func (h *Interceptor) Error() string

Jump to

Keyboard shortcuts

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