README
¶
httpcli
httcli
is a simple HTTP request client, which only supports returning json format.
Example of use
Request way 1
import "github.com/18721889353/sunshine/pkg/httpcli"
type User struct{
Name string
Email string
}
url := "http://localhost:8080/user"
params := httpcli.KV{"id":123}
headers := map[string]string{"Authorization": "Bearer token"}
body := &User{"foo", "foo@bar.com"}
result := &httpcli.StdResult{} // other structures can be defined to receive data
var err error
// Get
err = httpcli.Get(result, url)
err = httpcli.Get(result, url, httpcli.WithParams(params))
err = httpcli.Get(result, url, httpcli.WithParams(params), httpcli.WithHeaders(headers))
// Delete
err = httpcli.Delete(result, url)
err = httpcli.Delete(result, httpcli.WithParams(params))
err = httpcli.Delete(result, httpcli.WithParams(params), httpcli.WithHeaders(headers))
// Post
err = httpcli.Post(result, url, body)
err = httpcli.Post(result, url, body, httpcli.WithParams(params))
err = httpcli.Delete(result, httpcli.WithParams(params), httpcli.WithHeaders(headers))
// Put
err := httpcli.Put(result, url, body)
// Patch
err := httpcli.Patch(result, url, body)
Request way 2
Get, Delete request example.
import "github.com/18721889353/sunshine/pkg/httpcli"
url := "http://localhost:8080/user"
headers := map[string]string{"Authorization": "Bearer token"}
params := httpcli.KV{"id": 123}
cli := httpcli.New().SetURL(url).SetHeaders(headers).SetParams(params)
// Get
resp, err := cli.GET()
// Delete
// resp, err := cli.Delete()
defer resp.Body.Close()
result := &httpcli.StdResult{} // other structures can be defined to receive data
err = resp.BindJSON(result)
Post, Put, Patch request example.
import "github.com/18721889353/sunshine/pkg/httpcli"
type User struct{
Name string
Email string
}
body := &User{"foo", "foo@bar.com"}
url := "http://localhost:8080/user"
headers := map[string]string{"Authorization": "Bearer token"}
cli := httpcli.New().SetURL(url).SetHeaders(headers).SetBody(body)
// Post
resp, err := cli.Post()
// Put
// resp, err := cli.Put()
// Patch
// resp, err := cli.Patch()
defer resp.Body.Close()
result := &httpcli.StdResult{} // other structures can be defined to receive data
err = resp.BindJSON(result)
Documentation
¶
Overview ¶
Package httpcli is http request client, which only supports return json format.
Index ¶
- func Delete(result interface{}, urlStr string, opts ...Option) error
- func Get(result interface{}, urlStr string, opts ...Option) error
- func Patch(result interface{}, urlStr string, body interface{}, opts ...Option) error
- func Post(result interface{}, urlStr string, body interface{}, opts ...Option) error
- func Put(result interface{}, urlStr string, body interface{}, opts ...Option) error
- type KV
- type Option
- type Request
- func (req *Request) CustomRequest(f func(req *http.Request, data *bytes.Buffer)) *Request
- func (req *Request) DELETE() (*Response, error)
- func (req *Request) Do(method string, data interface{}) (*Response, error)
- func (req *Request) GET() (*Response, error)
- func (req *Request) PATCH() (*Response, error)
- func (req *Request) POST() (*Response, error)
- func (req *Request) PUT() (*Response, error)
- func (req *Request) Reset()
- func (req *Request) Response() (*Response, error)
- func (req *Request) SetBody(body interface{}) *Request
- func (req *Request) SetContentType(a string) *Request
- func (req *Request) SetHeader(k, v string) *Request
- func (req *Request) SetHeaders(headers map[string]string) *Request
- func (req *Request) SetParam(k string, v interface{}) *Request
- func (req *Request) SetParams(params map[string]interface{}) *Request
- func (req *Request) SetTimeout(t time.Duration) *Request
- func (req *Request) SetURL(path string) *Request
- type Response
- type StdResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
Request HTTP request
func (*Request) CustomRequest ¶
CustomRequest customize request, e.g. add sign, set header, etc.
func (*Request) Reset ¶
func (req *Request) Reset()
Reset set all fields to default value, use at pool
func (*Request) SetBody ¶
SetBody set body data, support string and []byte, if it is not string, it will be json marshal.
func (*Request) SetContentType ¶
SetContentType set ContentType
func (*Request) SetHeaders ¶
SetHeaders set the value of Request Headers
func (*Request) SetTimeout ¶
SetTimeout set timeout
Click to show internal directories.
Click to hide internal directories.