Documentation ¶
Overview ¶
Package client provides a HTTP client implementation. This client 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 the server-side request forgery (SSRF) security vulnerability.
func Unsafe ¶ added in v0.0.8
Unsafe creates a client that is NOT safe from the 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) // This is the AWS metadata url. url := "http://169.254.169.254/latest/meta-data" _, _ = cli.Get(context.Background(), url) // This will log something like: // {"level":"info","logID":"Z5X7qXm8HkT8kZ83xQyrrQ","method":"GET","msg":"http_client","pid":11776,"process":"request","timestamp":"2022-10-09T12:03:33.851543383Z","url":"http://169.254.169.254/latest/meta-data"} // {"durationMS":0,"err":"Get \"http://169.254.169.254/latest/meta-data\": dial tcp 169.254.169.254:80: ong/client: address 169.254.169.254 IsLinkLocalUnicast","level":"error","logID":"Z5X7qXm8HkT8kZ83xQyrrQ","method":"GET","msg":"http_client","pid":11776,"process":"response","timestamp":"2022-10-09T12:03:33.851889217Z","url":"http://169.254.169.254/latest/meta-data"} }
Output: