README
¶
gohttp
The http request client, which only supports returning json format.
Example of use
Standard CURD
Get, Delete request example.
import "github.com/18721889353/sunshine/pkg/gohttp"
req := gohttp.Request{}
req.SetURL("http://localhost:8080/user")
req.SetHeaders(map[string]string{
"Authorization": "Bearer token",
})
req.SetParams(gohttp.KV{
"id": 123,
})
resp, err := req.GET()
// resp, err := req.Delete()
result := &gohttp.StdResult{} // other structures can be defined to receive data
err = resp.BindJSON(result)
Post, Put, Patch request example.
import "github.com/18721889353/sunshine/pkg/gohttp"
req := gohttp.Request{}
req.SetURL("http://localhost:8080/user")
req.SetHeaders(map[string]string{
"Authorization": "Bearer token",
})
// body is a structure
type User struct{
Name string
Email string
}
body := &User{"foo", "foo@bar.com"}
req.SetJSONBody(body)
// or body as json
// req.SetBody(`{"name":"foo", "email":"foo@bar.com"}`)
resp, err := req.Post()
// resp, err := req.Put()
// resp, err := req.Patch()
result := &gohttp.StdResult{} // other structures can be defined to receive data
err = resp.BindJSON(result)
simplified version of CRUD
No support for setting header, timeout, etc.
import "github.com/18721889353/sunshine/pkg/gohttp"
url := "http://localhost:8080/user"
params := gohttp.KV{"id":123}
result := &gohttp.StdResult{} // other structures can be defined to receive data
// Get
err := gohttp.Get(result, url)
err := gohttp.Get(result, url, params)
// Delete
err := gohttp.Delete(result, url)
err := gohttp.Delete(result, url, params)
type User struct{
Name string
Email string
}
body := &User{"foo", "foo@bar.com"}
// Post
err := gohttp.Post(result, url, body)
// Put
err := gohttp.Put(result, url, body)
// Patch
err := gohttp.Patch(result, url, body)
Documentation
¶
Overview ¶
Package gohttp is http request client, which only supports returning json format. Deprecated: moved to pkg/httpcli, will remove in future version.
Index ¶
- func Delete(result interface{}, urlStr string, params ...KV) error
- func Get(result interface{}, urlStr string, params ...KV) error
- func Patch(result interface{}, urlStr string, body interface{}) error
- func Post(result interface{}, urlStr string, body interface{}) error
- func Put(result interface{}, urlStr string, body interface{}) error
- type KV
- 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 string) *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) SetJSONBody(body interface{}) *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 KV ¶
type KV = map[string]interface{}
KV string:interface{} Deprecated: moved to pkg/httpcli KV
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
Request HTTP request Deprecated: moved to pkg/httpcli Request
func (*Request) CustomRequest ¶
CustomRequest customize request, e.g. add sign, set header, etc. Deprecated: moved to pkg/httpcli CustomRequest
func (*Request) Reset ¶
func (req *Request) Reset()
Reset set all fields to default value, use at pool Deprecated: moved to pkg/httpcli Reset
func (*Request) SetContentType ¶
SetContentType set ContentType Deprecated: moved to pkg/httpcli SetContentType
func (*Request) SetHeader ¶
SetHeader set the value of the request header Deprecated: moved to pkg/httpcli SetHeader
func (*Request) SetHeaders ¶
SetHeaders set the value of Request Headers Deprecated: moved to pkg/httpcli SetHeaders
func (*Request) SetJSONBody ¶
SetJSONBody set Body data, JSON format Deprecated: moved to pkg/httpcli SetJSONBody
func (*Request) SetParam ¶
SetParam parameters after setting the URL Deprecated: moved to pkg/httpcli SetParam
func (*Request) SetParams ¶
SetParams parameters after setting the URL Deprecated: moved to pkg/httpcli SetParams
func (*Request) SetTimeout ¶
SetTimeout set timeout Deprecated: moved to pkg/httpcli SetTimeout
type Response ¶
Response HTTP response
func (*Response) BindJSON ¶
BindJSON parses the response's body as JSON Deprecated: moved to pkg/httpcli BindJSON
func (*Response) BodyString ¶
BodyString returns the body data of the HttpResponse Deprecated: moved to pkg/httpcli BodyString