Documentation ¶
Index ¶
- func Do(url string, opt ...Option) (*http.Response, error)
- func DoBytes(url string, opt ...Option) (data []byte, code int, err error)
- func Get(url string, opt ...Option) (*http.Response, error)
- func GetBytes(url string, opt ...Option) (data []byte, code int, err error)
- func Post(url, contentType string, body io.Reader, opt ...Option) (*http.Response, error)
- func PostBytes(url, contentType string, body io.Reader, opt ...Option) (data []byte, code int, err error)
- type Client
- func (c *Client) Do(url string, opt ...Option) (*http.Response, error)
- func (c *Client) DoBytes(url string, opt ...Option) (data []byte, code int, err error)
- func (c *Client) Get(url string, opt ...Option) (*http.Response, error)
- func (c *Client) GetBytes(url string, opt ...Option) (data []byte, code int, err error)
- func (c *Client) Post(url, contentType string, body io.Reader, opt ...Option) (*http.Response, error)
- func (c *Client) PostBytes(url, contentType string, body io.Reader, opt ...Option) (data []byte, code int, err error)
- type Config
- type Option
- func WithAddCookie(cookie *http.Cookie) Option
- func WithBodyBytes(contentType string, data []byte) Option
- func WithBodyReader(contentType string, body io.Reader) Option
- func WithBodyString(contentType string, body string) Option
- func WithCheckStatus(check bool) Option
- func WithContext(ctx context.Context) Option
- func WithHeader(header http.Header) Option
- func WithMethod(method string) Option
- func WithMultipart(params map[string]string) Option
- func WithMultipartFile(fieldname, filename string, data []byte, params ...map[string]string) Option
- func WithPostForm(params map[string]string) Option
- func WithPostJSON(v interface{}) Option
- func WithQuery(params map[string]string) Option
- func WithQueryValue(key, value string) Option
- func WithRequest(req *http.Request) Option
- func WithSetHeader(k, v string) Option
- type Options
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Do ¶
Do method construct a HTTP request with options Example:
resp, err := Do("http://localhost/api",
WithPostJSON(v), WithCheckStatus(true))
and return the *http.Response.
func DoBytes ¶
DoBytes method construct a HTTP request with options and return the bytes of resp.Body and http.StatusCode
func GetBytes ¶
GetBytes issues GET with options to the specified URL and return the bytes of the resp.Body.
func Post ¶
Post issues a POST with options to the specified URL and return *http.Response. This method is compatible with the original usage please use Do or DoBytes as much as possible.
func PostBytes ¶
func PostBytes(url, contentType string, body io.Reader, opt ...Option) (data []byte, code int, err error)
PostBytes issues a POST with options to the specified URL and return the bytes of the resp.Body. This method is compatible with the original usage please use Do or DoBytes as much as possible.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client wraps a HTTP Client that support functional options and make HTTP requests easier. It also compatible with the http.Client.
func (*Client) Do ¶
Do method construct a HTTP request with options Example:
resp, err := Do("http://localhost/api",
WithPostJSON(v), WithCheckStatus(true))
and return the *http.Response.
func (*Client) DoBytes ¶
DoBytes method construct a HTTP request with options and return the bytes of resp.Body and http.StatusCode.
func (*Client) GetBytes ¶
GetBytes issues GET with options to the specified URL and return the bytes of the resp.Body.
func (*Client) Post ¶
func (c *Client) Post(url, contentType string, body io.Reader, opt ...Option) (*http.Response, error)
Post issues a POST with options to the specified URL and return *http.Response. This method is compatible with the original usage please use Do or DoBytes as much as possible.
func (*Client) PostBytes ¶
func (c *Client) PostBytes(url, contentType string, body io.Reader, opt ...Option) (data []byte, code int, err error)
PostBytes issues a POST with options to the specified URL and return the bytes of the resp.Body. This method is compatible with the original usage please use Do or DoBytes more likely.
type Config ¶
type Config struct { Timeout time.Duration Transport http.RoundTripper }
Config defines a config for Client.
type Option ¶
type Option func(o *Options)
Option is a type define use for pass closure as parameters.
func WithBodyBytes ¶
WithBodyBytes set []byte into the request body.
func WithBodyReader ¶
WithBodyReader set io.Reader into the request body.
func WithBodyString ¶
WithBodyString set string into the request body.
func WithCheckStatus ¶
WithCheckStatus treat non-200 as error NOTE it only effected which method with bytes return, method with *http.Response return does not effected.
func WithContext ¶
WithContext set context to the http.Request it use to timeout or cancel.
Example:
ctx, cancel := context.WithCancel(context.Background(), time.Second*3) defer cancel() data, _, err := xreq.GetBytes(url,
WithContext(ctx),
)
func WithHeader ¶
WithHeader set up the entire http.Header.
func WithMultipart ¶
WithMultipart set the multipart/form-data without file.
func WithMultipartFile ¶
WithWithMultipartFile use multipart/form-data format to upload file.
func WithPostForm ¶
WithPostForm set the entire post form
func WithPostJSON ¶
func WithPostJSON(v interface{}) Option
WithPostJSON marshal v to the JSON bytes and set to the request body.
func WithQueryValue ¶
WithQueryValue set key-value into query. Example:
body, err := Get("http://localhost/api",
WithQuery("name", "jack"), WithQuery("id", "18"))
and the request URL will be "http://localhost/api?name=jack&id=18"
func WithRequest ¶
WithRequest replace the http.Request entirely.
func WithSetHeader ¶
WithSetHeader set key-value into http.Header.