A simple and flexible HTTP client for Go that automatically handles JSON marshaling/unmarshaling with support for custom options.
- Simple HTTP methods: GET, POST, PUT, PATCH, DELETE
- Automatic JSON marshaling and unmarshaling
- Customizable marshal/unmarshal functions
- Request options (headers, status capture)
- Built on Go's standard
net/httppackage
go get github.com/llkhacquan/httpclienttype Client struct {
Client *http.Client // HTTP client (defaults to http.DefaultClient)
MarshalFunc func(v any) ([]byte, error) // JSON marshal function (defaults to json.Marshal)
UnmarshalFunc func(data []byte, v any) error // JSON unmarshal function (defaults to json.Unmarshal)
}Get(ctx context.Context, url string, result interface{}, opts ...Option) errorPost(ctx context.Context, url string, body interface{}, result interface{}, opts ...Option) errorPut(ctx context.Context, url string, body interface{}, result interface{}, opts ...Option) errorPatch(ctx context.Context, url string, body interface{}, result interface{}, opts ...Option) errorDelete(ctx context.Context, url string, result interface{}, opts ...Option) error
WithHeader(key, value string) Option- Add a custom headerWithHeaders(headers map[string]string) Option- Add multiple headersWithStatus(status *int) Option- Capture HTTP status code and allow non-200 responses
Run the tests:
go test -vThe tests use real APIs (Pokemon API and httpbin.org) to ensure the client works correctly with actual HTTP services.
This package was built with assistance from Claude Code by Anthropic.
MIT License