func wrapCtx(handler func(ctx *gin.Context)) gin.HandlerFunc {
    return func(c *gin.Context) {
        //获取请求的url
        log.Info("当前请求url:%s", c.Request.RequestURI)
        token := c.GetHeader("token")
        log.Info("当前请求token:%s", token)
        //获取请求的body内容
        data, err := c.GetRawData()
        if err != nil {
            fmt.Println(err.Error())
        }
        if data != nil {
            log.Info("请求body内容为:%s", data)
        }
        //很关键
        //把读过的字节流重新放到body
        c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(data))
        //获取请求的body内容
        //body, _ := ioutil.ReadAll(c.Request.Body)
        //if body != nil {
        //    log.Info("请求body内容为:%s", body)
        //}
        //获取所有请求的post参数
        //有文件时,这里会有bug
        c.Request.ParseMultipartForm(200000)
        c.Request.ParseForm()
        postStr := ""
        for k, v := range c.Request.PostForm {
            postStr += fmt.Sprintf("%v:%v\t", k, v)
        }
        if postStr != "" {
            log.Info("post数据:%s", postStr)
        }
        handler(c)
    }
}


r.POST("/hello", wrapCtx(test.Hello))
 

相关文章:

  • 2021-06-23
  • 2021-07-01
  • 2021-07-28
  • 2021-05-08
  • 2021-05-21
  • 2021-12-01
猜你喜欢
  • 2021-05-01
  • 2021-10-16
  • 2021-07-30
  • 2021-05-14
  • 2021-07-01
  • 2021-10-02
相关资源
相似解决方案