var v struct {
Result interface{} `json:"result"`
Success bool `json:"success"`
Error interface{} `json:"error"`
}
statusCode, err := httpreq.New(http.MethodGet, "http://127.0.0.1", nil).
Call(&v)
fmt.Println(statusCode, err,v)
var v struct {
Result interface{} `json:"result"`
Success bool `json:"success"`
Error interface{} `json:"error"`
}
statusCode, err := httpreq.New(http.MethodGet, "http://127.0.0.1", nil).
WithToken("token-1").
WithRequestID("requestID-1").
WithActionID("actionID-1").
Call(&v)
body :=`{
"price":12
}`
statusCode, err := httpreq.New(http.MethodPost, "http://127.0.0.1", body).
WithToken("token-1").
WithBehaviorLogContext(behaviorlog.FromCtx(ctx)).
Call(&v)
statusCode, err := httpreq.New(http.MethodGet, "http://127.0.0.1", nil).
Call(&v)
body :=`{
"price":12
}`
statusCode, err := httpreq.New(http.MethodPost, "http://127.0.0.1", body).
Call(&v)
- Declare global
*http.Client
variable
var client = httpreq.NewClient(httpreq.ClientConfig{
Timeout: time.Second,
})
- Call with
*http.Client
_, err := httpreq.New(http.MethodGet, url, nil).CallWithClient(&resp, client)
Use https://github.com/matryer/try
- Import
try
package
import try "gopkg.in/matryer/try.v1"
- Use
try
package
err := try.Do(func(attempt int) (bool, error) {
_, err := httpreq.New(http.MethodGet, url, nil).Call(&resp)
return attempt < 5, err // try 5 times
})
The types of request and response support are as follows:
- JsonType
- FormType
- XmlType
- ByteArrayType
var v struct {
Result interface{} `json:"result"`
Success bool `json:"success"`
Error interface{} `json:"error"`
}
statusCode, err := httpreq.New(http.MethodGet, "http://127.0.0.1", nil, func(httpReq *httpreq.HttpReq) error {
httpReq.RespDataType = httpreq.JsonType
return nil
}).
Call(&v)
body :=`{
"price":12
}`
statusCode, err := httpreq.New(http.MethodPost, "http://127.0.0.1", body, func(httpReq *httpreq.HttpReq) error {
httpReq.ReqDataType = httpreq.FormType
httpReq.RespDataType = httpreq.XmlType
return nil
}).
Call(&v)