Documentation ¶
Overview ¶
Package client provides HTTP client implementation. The client provided in here is opinionated and comes with good defaults.
Index ¶
- type Client
- func (c *Client) CloseIdleConnections()
- func (c *Client) Do(ctx context.Context, req *http.Request) (resp *http.Response, err error)
- func (c *Client) Get(ctx context.Context, url string) (resp *http.Response, err error)
- func (c *Client) Head(ctx context.Context, url string) (resp *http.Response, err error)
- func (c *Client) Post(ctx context.Context, url, contentType string, body io.Reader) (resp *http.Response, err error)
- func (c *Client) PostForm(ctx context.Context, url string, data url.Values) (resp *http.Response, err error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a http.Client that has some good defaults. It also logs requests and responses using log.Logger
Use either Safe or Unsafe to get a valid client.
Clients should be reused instead of created as needed. Clients are safe for concurrent use by multiple goroutines.
see http.Client
func Safe ¶ added in v0.0.8
Safe creates a client that is safe from Server-side request forgery (SSRF) security vulnerability.
func Unsafe ¶ added in v0.0.8
Unsafe creates a client that is NOT safe from Server-side request forgery (SSRF) security vulnerability.
func (*Client) CloseIdleConnections ¶
func (c *Client) CloseIdleConnections()
CloseIdleConnections closes any connections on its Transport which were previously connected from previous requests but are now sitting idle in a "keep-alive" state.
It does not interrupt any connections currently in use.
func (*Client) Do ¶
Do sends an HTTP request and returns an HTTP response, following policy (such as redirects, cookies, auth) as configured on the client.
see http.Client.Do
func (*Client) Get ¶
Get issues a GET to the specified URL.
see http.Client.Get
Example ¶
package main import ( "context" "os" "github.com/komuw/ong/client" "github.com/komuw/ong/log" ) func main() { l := log.New(os.Stdout, 7) cli := client.Safe(l) _, _ = cli.Get(context.Background(), "https://ajmsmsYnns-bad-domain.com") // This will log: // {"level":"info","logID":"D2MH3e3BqZmRgm3WK8yK7Q","method":"GET","msg":"http_client","pid":2102616,"process":"request","timestamp":"2022-09-16T09:39:55.423743309Z","url":"https://ajmsmsYnns-bad-domain.com"} // {"durationMS":339,"err":"Get \"https://ajmsmsYnns-bad-domain.com\": dial tcp: lookup ajmsmsYnns-bad-domain.com on 127.0.0.53:53: no such host","level":"error","logID":"D2MH3e3BqZmRgm3WK8yK7Q","method":"GET","msg":"http_client","pid":2102616,"process":"response","timestamp":"2022-09-16T09:39:55.762989726Z","url":"https://ajmsmsYnns-bad-domain.com"} }
Output: