HTTP client

Call another JSON service in two lines, with a bearer token and query parameters.

Import
github.com/HiWay-Media/hwm-go-utils/api/client
Built on
resty v2

Usage

svc := client.NewService(logger, "https://billing.internal/api")
svc.SetAuthorization(accessToken) // sent as "Authorization: Bearer …" on every request

resp, err := svc.Get("/invoices", map[string]string{"customer": "42", "status": "open"})
if err != nil {
	return err // network error
}
if resp.IsError() {
	return fmt.Errorf("billing: %s", resp.Status())
}

var invoices []Invoice
if err := json.Unmarshal(resp.Body(), &invoices); err != nil {
	return err
}

Methods

Method Body Notes
Get(route, params) — params become the query string
Delete(route, params) —  
Post(route, body, params) JSON Content-Type: application/json is set when body != nil
Put(route, body, params) JSON  
SetAuthorization(token) — Pass "" to stop sending the header

All methods return the raw *resty.Response: check resp.IsError() / resp.StatusCode() yourself — an HTTP 4xx/5xx is not returned as a Go error.

At debug level the client logs request and response bodies. Keep production loggers at info or above if those bodies can contain personal data or secrets.