内容简介:项目地址 https://github.com/guonaihong/gout https://gitee.com/guonaihong/gout 本次更新 支持响应中间件 修复host写全url, 内容不对的问题 响应中间件例子 import ( "bytes" "encoding/json" "errors...
项目地址
https://github.com/guonaihong/gout
https://gitee.com/guonaihong/gout
本次更新
- 支持响应中间件
- 修复host写全url, 内容不对的问题
响应中间件例子
import ( "bytes" "encoding/json" "errors" "fmt" "github.com/gin-gonic/gin" "github.com/guonaihong/gout" "io/ioutil" "log" "net/http" "time" ) // response拦截器修改示例 type demoResponseMiddler struct{} func (d *demoResponseMiddler) ModifyResponse(response *http.Response) error { // 修改responseBody。 因为返回值大概率会有 { code, data,msg} 等字段,希望进行统一处理 //这里想验证code. 如果不对就返回error。 对的话将 data中的内容写入body,这样后面BindJson的时候就可以直接处理业务了 all, err := ioutil.ReadAll(response.Body) if err != nil { return err } obj := make(map[string]interface{}) err = json.Unmarshal(all, &obj) if err != nil { return err } code := obj["code"] msg := obj["msg"] data := obj["data"] // Go中json中的数字经过反序列化会成为float64类型 if float64(200) != code { return errors.New(fmt.Sprintf("请求失败, code %d msg %s", code, msg)) } else { byt, _ := json.Marshal(&data) response.Body = ioutil.NopCloser(bytes.NewReader(byt)) return nil } } func demoResponse() *demoResponseMiddler { return &demoResponseMiddler{} } func main() { go server() //等会起测试服务 time.Sleep(time.Millisecond * 500) //用时间做个等待同步 responseUseExample() } func responseUseExample() { //成功请求 successRes := new(map[string]interface{}) err := gout.GET(":8080/success").ResponseUse(demoResponse()).BindJSON(&successRes).Do() log.Printf("success请求 --> 响应 %s \n err %s \n ", successRes, err) //fail请求 failRes := new(map[string]interface{}) err = gout.GET(":8080/fail").ResponseUse(demoResponse()).BindJSON(&failRes).Do() log.Printf("fail请求 --> 响应 %s \n err %s \n ", successRes, err) } type Result struct { Code int `json:"code"` Msg string `json:"msg"` Data interface{} `json:"data"` } type Item struct { Id string `json:"id"` Name string `json:"name"` } func server() { router := gin.New() router.GET("/success", func(c *gin.Context) { c.JSON(200, Result{200, "请求成功了", Item{"001", "张三"}}) }) router.GET("/fail", func(c *gin.Context) { c.JSON(200, Result{500, "查询数据库出错了", nil}) }) router.Run() }
feature
- 支持设置 GET/PUT/DELETE/PATH/HEAD/OPTIONS
- 支持设置请求 http header(可传 struct,map,array,slice 等类型)
- 支持设置 URL query(可传 struct,map,array,slice,string 等类型)
- 支持设置 json 编码到请求 body 里面(可传struct, map, string, []byte 等类型)
- 支持设置 xml 编码到请求 body 里面(可传struct, string, []byte 等类型)
- 支持设置 yaml 编码到请求 body 里面(可传struct, map, string, []byte 等类型)
- 支持设置 protobuf 编码到请求 body里面(可传struct)
- 支持设置 form-data(可传 struct, map, array, slice 等类型)
- 支持设置 x-www-form-urlencoded(可传 struct,map,array,slice 等类型)
- 支持设置 io.Reader,uint/uint8/uint16...int/int8...string...[]byte...float32,float64 至请求 body 里面
- 支持解析响应body里面的json,xml,yaml至结构体里(BindJSON/BindXML/BindYAML)
- 支持解析响应body的内容至io.Writer, uint/uint8...int/int8...string...[]byte...float32,float64
- 支持解析响应header至结构体里
- 支持接口性能benchmark,可控制压测一定次数还是时间,可控制压测频率
- 支持retry-backoff,可以指定重试条件
- 支持发送裸http数据包
- 支持导出curl命令
- 传入自定义*http.Client
- 支持请求中间件(https://github.com/antlabs/gout-middleware)
- 支持响应中间件ResponseUse
- 支持设置chunked数据格式发送
- 支持body, header的数据校验
- 等等更多
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Django 1.0 Template Development
Scott Newman / Packt / 2008 / 24.99
Django is a high-level Python web application framework designed to support the rapid development of dynamic websites, web applications, and web services. Getting the most out of its template system a......一起来看看 《Django 1.0 Template Development》 这本书的介绍吧!