Documentation ¶
Overview ¶
Package gorilla/http is a high level HTTP client.
This package provides high level convience methods for common http operations. Additionally a high level HTTP client implementation.
These high level functions are expected to change. Your feedback on their form and utility is warmly requested.
Please raise issues at https://github.com/gorilla/http/issues.
For lower level http implementations, see gorilla/http/client.
Index ¶
- Variables
- func Get(w io.Writer, url string) (int64, error)
- func Post(url string, r io.Reader) error
- type Client
- func (c *Client) Do(method, url string, headers map[string][]string, body io.Reader) (client.Status, map[string][]string, io.ReadCloser, error)
- func (c *Client) Get(url string, headers map[string][]string) (client.Status, map[string][]string, io.ReadCloser, error)
- func (c *Client) Post(url string, headers map[string][]string, body io.Reader) (client.Status, map[string][]string, io.ReadCloser, error)
- type Conn
- type Dialer
- type StatusError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultCachingDialer = NewCachingDialer(DefaultDialer)
DefaultCachingDialer is simply the DefaultDialer wrapped with a cache.
var DefaultClient = Client{ Dialer: DefaultCachingDialer, FollowRedirects: true, }
DefaultClient is the default http Client used by this package. It's defaults are expected to represent the best practice at the time, but may change over time. If you need more control or reproducibility, you should construct your own client.
var DefaultDialer = dialer{}
DefaultDialer is a non-caching dialer for a Client. It is strict with HTTPS certificates
var InsecureDialer = dialer{/* contains filtered or unexported fields */}
InsecureDialer is a non-caching dialer for a Client. It does not verify peer certificates nor does it validate hostnames.
Functions ¶
func Get ¶
Get issues a GET request using the DefaultClient and writes the result to to w if successful. If the status code of the response is not a success (see Success.IsSuccess()) no data will be written and the status code will be returned as an error.
Example ¶
// curl in 3 lines of code. if _, err := Get(os.Stdout, "http://www.gorillatoolkit.org/"); err != nil { log.Fatalf("could not fetch: %v", err) }
Output:
func Post ¶
Post issues a POST request using the DefaultClient using r as the body. If the status code was not a success code, it will be returned as an error.
Example ¶
// send the contents of os.Stdin to a remote webserver. if err := Post("http://www.example.com", os.Stdin); err != nil { log.Fatalf("could not post: %v", err) }
Output:
Types ¶
type Client ¶
type Client struct { Dialer Dialer // FollowRedirects instructs the client to follow 301/302 redirects when idempotent. FollowRedirects bool }
Client implements a high level HTTP client. Client methods can be called concurrently to as many end points as required.
func (*Client) Do ¶
func (c *Client) Do(method, url string, headers map[string][]string, body io.Reader) (client.Status, map[string][]string, io.ReadCloser, error)
Do sends an HTTP request and returns an HTTP response. If the response body is non nil it must be closed.
func (*Client) Get ¶
func (c *Client) Get(url string, headers map[string][]string) (client.Status, map[string][]string, io.ReadCloser, error)
Get sends a GET request. If the response body is non nil it must be closed.
func (*Client) Post ¶
func (c *Client) Post(url string, headers map[string][]string, body io.Reader) (client.Status, map[string][]string, io.ReadCloser, error)
Post sends a POST request, suppling the contents of the reader as the request body.
Example ¶
// send the contents of os.Stdin to a remote webserver. status, _, r, err := DefaultClient.Post("http://www.example.com", nil, os.Stdin) if err != nil { log.Fatal(err) } if r != nil { defer r.Close() } log.Printf("Post result: %v", status)
Output:
type Conn ¶
type Conn interface { client.Client io.Closer SetDeadline(time.Time) error SetReadDeadline(time.Time) error SetWriteDeadline(time.Time) error // Release returns the Conn to the Dialer for reuse. Release() }
Conn represnts a connection which can be used to communicate with a remote HTTP server.
type Dialer ¶
type Dialer interface { // Dial dials a remote http server returning a Conn having been requested // using the given http scheme Dial(scheme, host string) (Conn, error) }
Dialer can dial a remote HTTP server.
func NewCachingDialer ¶
NewCachingDialer takes an existing dialer and essentially memoizes the Dial method with inactive connections.
type StatusError ¶
StatusError reprents a client.Status as an error.
func (*StatusError) Error ¶
func (s *StatusError) Error() string