httpclient

package
v0.0.11 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultTransport

func DefaultTransport() *http.Transport

DefaultTransport returns a new *http.Transport with all default settings. Use this when wrapping the transport in a custom RoundTripper while keeping the baseline configuration:

client := &http.Client{
    Transport: myLoggingRoundTripper{next: httpclient.DefaultTransport()},
}

func FormPatch added in v0.0.11

func FormPatch(ctx context.Context, url string, fields map[string]string, files []FormFile, headers map[string]string) (int, []byte, error)

FormPatch sends a multipart/form-data PATCH request using the package-level default client.

func FormPost added in v0.0.11

func FormPost(ctx context.Context, url string, fields map[string]string, files []FormFile, headers map[string]string) (int, []byte, error)

FormPost sends a multipart/form-data POST request using the package-level default client. fields contains plain text form fields; files contains file uploads. Either may be nil.

func FormPut added in v0.0.11

func FormPut(ctx context.Context, url string, fields map[string]string, files []FormFile, headers map[string]string) (int, []byte, error)

FormPut sends a multipart/form-data PUT request using the package-level default client.

func JSONDelete

func JSONDelete(ctx context.Context, url string, headers map[string]string) (int, []byte, error)

func JSONGet

func JSONGet(ctx context.Context, url string, headers map[string]string) (int, []byte, error)

func JSONHead

func JSONHead(ctx context.Context, url string, headers map[string]string) (int, []byte, error)

func JSONOptions

func JSONOptions(ctx context.Context, url string, headers map[string]string) (int, []byte, error)

func JSONPatch

func JSONPatch(ctx context.Context, url string, body any, headers map[string]string) (int, []byte, error)

func JSONPost

func JSONPost(ctx context.Context, url string, body any, headers map[string]string) (int, []byte, error)

func JSONPut

func JSONPut(ctx context.Context, url string, body any, headers map[string]string) (int, []byte, error)

func NewClient

func NewClient(client *http.Client) *http.Client

NewClient populates zero/nil fields in client with production-ready defaults and returns it. Pass nil to get a fully-defaulted client.

Transport handling:

  • nil Transport → a new *http.Transport is created (ForceAttemptHTTP2=true).
  • *http.Transport → only zero-value duration/size fields are filled; boolean fields are left untouched so explicit caller choices are kept.
  • other RoundTripper → used as-is.

func SendFormRequest added in v0.0.11

func SendFormRequest(ctx context.Context, method, url string, fields map[string]string, files []FormFile, headers map[string]string) (int, []byte, error)

SendFormRequest executes a multipart/form-data request using the package-level default client. ctx controls cancellation and deadlines; pass context.Background() when no deadline propagation is needed.

func SendRequest

func SendRequest(ctx context.Context, method, url string, body any, headers map[string]string) (int, []byte, error)

SendRequest executes an HTTP request using the package-level default client. ctx controls cancellation and deadlines; pass context.Background() when no deadline propagation is needed.

func SetDefaultClient

func SetDefaultClient(client *http.Client)

SetDefaultClient replaces the package-level client used by SendRequest and all JSON* convenience functions. It is safe to call concurrently. Pass nil to reset to the built-in defaults.

Prefer creating an *HTTPClient instance (NewHTTPClient) over replacing the global when different parts of your application need different settings.

Types

type FormFile added in v0.0.11

type FormFile struct {
	FieldName string    // form field name (e.g. "avatar")
	FileName  string    // file name sent to the server (e.g. "photo.png")
	Content   io.Reader // file content
}

FormFile represents a file to include in a multipart/form-data request.

type HTTPClient

type HTTPClient struct {
	// contains filtered or unexported fields
}

HTTPClient wraps an *http.Client and mirrors the package-level API. Use this when different parts of your application need separate clients (e.g. different timeouts or TLS config) rather than sharing the global.

c := httpclient.NewHTTPClient(&http.Client{Timeout: 5 * time.Minute})
status, body, err := c.JSONPost(ctx, url, payload, nil)

func NewHTTPClient

func NewHTTPClient(client *http.Client) *HTTPClient

NewHTTPClient wraps client with production defaults applied via NewClient. Pass nil to start from all defaults.

func (*HTTPClient) FormPatch added in v0.0.11

func (hc *HTTPClient) FormPatch(ctx context.Context, url string, fields map[string]string, files []FormFile, headers map[string]string) (int, []byte, error)

func (*HTTPClient) FormPost added in v0.0.11

func (hc *HTTPClient) FormPost(ctx context.Context, url string, fields map[string]string, files []FormFile, headers map[string]string) (int, []byte, error)

func (*HTTPClient) FormPut added in v0.0.11

func (hc *HTTPClient) FormPut(ctx context.Context, url string, fields map[string]string, files []FormFile, headers map[string]string) (int, []byte, error)

func (*HTTPClient) JSONDelete

func (hc *HTTPClient) JSONDelete(ctx context.Context, url string, headers map[string]string) (int, []byte, error)

func (*HTTPClient) JSONGet

func (hc *HTTPClient) JSONGet(ctx context.Context, url string, headers map[string]string) (int, []byte, error)

func (*HTTPClient) JSONHead

func (hc *HTTPClient) JSONHead(ctx context.Context, url string, headers map[string]string) (int, []byte, error)

func (*HTTPClient) JSONOptions

func (hc *HTTPClient) JSONOptions(ctx context.Context, url string, headers map[string]string) (int, []byte, error)

func (*HTTPClient) JSONPatch

func (hc *HTTPClient) JSONPatch(ctx context.Context, url string, body any, headers map[string]string) (int, []byte, error)

func (*HTTPClient) JSONPost

func (hc *HTTPClient) JSONPost(ctx context.Context, url string, body any, headers map[string]string) (int, []byte, error)

func (*HTTPClient) JSONPut

func (hc *HTTPClient) JSONPut(ctx context.Context, url string, body any, headers map[string]string) (int, []byte, error)

func (*HTTPClient) SendFormRequest added in v0.0.11

func (hc *HTTPClient) SendFormRequest(ctx context.Context, method, url string, fields map[string]string, files []FormFile, headers map[string]string) (int, []byte, error)

SendFormRequest executes a multipart/form-data request using this instance's client.

func (*HTTPClient) SendRequest

func (hc *HTTPClient) SendRequest(ctx context.Context, method, url string, body any, headers map[string]string) (int, []byte, error)

SendRequest executes an HTTP request using this instance's client. ctx controls cancellation and deadlines; pass context.Background() when no deadline propagation is needed.

Jump to

Keyboard shortcuts

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