Documentation ¶
Index ¶
- Constants
- Variables
- type Client
- func (c *Client) BasicAuth(username, password string) *Client
- func (c *Client) Body(body io.Reader) *Client
- func (c *Client) Cookie(cookie *http.Cookie) *Client
- func (c *Client) Debug(debug bool) *Client
- func (c *Client) Delete(urls ...string) (*GoResponse, error)
- func (c *Client) Do(method string, urls ...string) (*GoResponse, error)
- func (c *Client) File(f *os.File, fileName, fieldName string) *Client
- func (c *Client) Form(bodyForm interface{}) *Client
- func (c *Client) Get(urls ...string) (*GoResponse, error)
- func (c *Client) Head(urls ...string) (*GoResponse, error)
- func (c *Client) Header(key, value string) *Client
- func (c *Client) JSON(bodyJSON string) *Client
- func (c *Client) JSONStruct(bodyJSON interface{}) *Client
- func (c *Client) New() *Client
- func (c *Client) Options(urls ...string) (*GoResponse, error)
- func (c *Client) Patch(urls ...string) (*GoResponse, error)
- func (c *Client) Path(paths ...string) *Client
- func (c *Client) Post(urls ...string) (*GoResponse, error)
- func (c *Client) Proxy(proxy string) *Client
- func (c *Client) Put(urls ...string) (*GoResponse, error)
- func (c *Client) Query(key, value string) *Client
- func (c *Client) QueryStruct(queryStruct interface{}) *Client
- func (c *Client) Retries(n int) *Client
- func (c *Client) TLSHandshakeTimeout(timeout time.Duration) *Client
- func (c *Client) Timeout(timeout time.Duration) *Client
- func (c *Client) URL(url string) *Client
- type GoResponse
- func Delete(url string) (*GoResponse, error)
- func Get(url string) (*GoResponse, error)
- func Head(url string) (*GoResponse, error)
- func Options(url string) (*GoResponse, error)
- func Patch(url string, data io.Reader) (*GoResponse, error)
- func Post(url string, data io.Reader) (*GoResponse, error)
- func Put(url string, data io.Reader) (*GoResponse, error)
Constants ¶
const DefaultTimeout = 3 * time.Second
DefaultTimeout defines the request timeout limit, avoiding client hanging when the remote server does not responde.
Variables ¶
var DefaultClient = New()
DefaultClient provides a simple usable client, it is given for quick usage. For more control, please create a client manually.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is the main struct that wraps net/http. It stores all necessary data for the request to be sent, include request method, url, body.
func (*Client) Body ¶
Body accepts `io.Reader`, will read data from it and use it as request body. This doee not set `Content-Type` header, so users should use `Header(key, value)` to specify it if necessary.
func (*Client) Debug ¶
Debug sets the debug mode of go http. By default, debug mode is off.
This is only for testing and debugging purpose, it will print out detail information such as request and response dump string, and other logging lines.
If `GOHTTP_DEBUG` environment variable is set, `gohttp` will use the value. 1 for turn on debug mode, and others for turn off debug mode. Debug method overrides environment variable value.
func (*Client) Delete ¶
func (c *Client) Delete(urls ...string) (*GoResponse, error)
Delete handles HTTP DELETE request
func (*Client) Do ¶
func (c *Client) Do(method string, urls ...string) (*GoResponse, error)
Do takes HTTP and url, then makes the request, return the response. All other HTTP methods will call `Do` behind the scene, and it can be used directly to send the request. Custom HTTP method can be sent with this method. Accept optional url parameter, if multiple urls are given only the first one will be used.
func (*Client) File ¶
File adds a file in request body, and sends it to server Multiple files can be added by calling this method many times
func (*Client) Form ¶
Form accepts a struct, uses it as body data, and sent it as application/www-x-form-urlencoded If the actual method does not support body or form data, such as `GET`, `HEAD`, it will be simply omitted.
func (*Client) Get ¶
func (c *Client) Get(urls ...string) (*GoResponse, error)
Get handles HTTP GET request, and return response to user Note that the response is not `http.Response`, but a thin wrapper which does exactly what it used to and a little more.
func (*Client) Head ¶
func (c *Client) Head(urls ...string) (*GoResponse, error)
Head handles HTTP HEAD request HEAD request works the same way as GET, except the response body is empty.
func (*Client) JSON ¶
JSON accepts a string as data, and sets it as body, and send it as application/json If the actual method does not support body or json data, such as `GET`, `HEAD`, it will be simply omitted.
func (*Client) JSONStruct ¶
JSONStruct accepts a struct as data, and sets it as body, and send it as application/json If the actual method does not support body or json data, such as `GET`, `HEAD`, it will be simply omitted.
func (*Client) New ¶
New clones current client struct and returns it This is useful to initialize some common parameters and send different requests with differenet paths/headers/...
For example:
c := gohttp.New().URL("https://api.github.com/") c.BasicAuth("cizixs", "mypassword") users, err := c.New().Path("/users/").Get() repos, err := c.New().Path("/repos").Get()
Note that files, body and cookies value are copied if pointer value is used, base client and cloned client(s) will share the same instance, change on one side will take effect on the other side.
func (*Client) Options ¶
func (c *Client) Options(urls ...string) (*GoResponse, error)
Options handles HTTP OPTIONS request
func (*Client) Patch ¶
func (c *Client) Patch(urls ...string) (*GoResponse, error)
Patch handles HTTP PATCH request
func (*Client) Path ¶
Path concatenates base url with resource path. Path can be with or without slash `/` at both end, it will be handled properly.
Usage:
gohttp.New().Path("users/cizixs").Get("someurl.com")
func (*Client) Post ¶
func (c *Client) Post(urls ...string) (*GoResponse, error)
Post handles HTTP POST request
func (*Client) Proxy ¶
Proxy sets proxy server the client uses. If it is empty, `gohttp` will try to load proxy settings from environment variable
Usage:
gohttp.New().Proxy("http://127.0.0.1:4567").Get("http://someurl.com")
func (*Client) Put ¶
func (c *Client) Put(urls ...string) (*GoResponse, error)
Put handles HTTP PUT request
func (*Client) QueryStruct ¶
QueryStruct parses a struct as query strings On how it works, please refer to github.com/google/go-querystring repo
func (*Client) Retries ¶
Retries set how many request attempts will be conducted if error happens for a request. number <= 1 means no retries, send one request and finish.
func (*Client) TLSHandshakeTimeout ¶
TLSHandshakeTimeout sets the wait limit for performing TLS handshake. Default value for go1.6 is 10s, change this value to suit yourself.
func (*Client) Timeout ¶
Timeout sets the wait limit for a request to finish. This time includes connection time, redirectoin time, and read response body time. If request does not finish before the timeout, any ongoing action will be interrupted and an error will return
Usage:
gohttp.New().Timeout(time.Second * 10).Get(url)
type GoResponse ¶
GoResponse wraps the official `http.Response`, and provides more features. The main function is to parse resp body for users. In the future, it can gives more information, like request elapsed time, redirect history etc.
func Delete ¶
func Delete(url string) (*GoResponse, error)
Delete provides a shortcut to send `DELETE` request It is used to remove a resource from server
func Head ¶
func Head(url string) (*GoResponse, error)
Head provides a shortcut to send `HEAD` request
func Options ¶
func Options(url string) (*GoResponse, error)
Options provides a shortcut to send `OPTIONS` request
func Patch ¶
func Patch(url string, data io.Reader) (*GoResponse, error)
Patch provides a shortcut to send `PATCH` request
func Post ¶
func Post(url string, data io.Reader) (*GoResponse, error)
Post provides a shortcut to send `POST` request
func Put ¶
func Put(url string, data io.Reader) (*GoResponse, error)
Put provides a shortcut to send `PUT` request
func (*GoResponse) AsBytes ¶
func (resp *GoResponse) AsBytes() ([]byte, error)
AsBytes return the response body as byte slice An error will be returned if the body can not be read as bytes
func (*GoResponse) AsJSON ¶
func (resp *GoResponse) AsJSON(v interface{}) error
AsJSON parses response body to a struct Usage:
user := &User{} resp.AsJSON(user) fmt.Printf("%s\n", user.Name)
func (*GoResponse) AsString ¶
func (resp *GoResponse) AsString() (string, error)
AsString returns the response data as string An error wil bw returned if the body can not be read as string