支持将请求信息写入上下文

This commit is contained in:
白茶清欢 2022-07-14 11:08:03 +08:00
parent 6e455f43af
commit a2cf256380
1 changed files with 9 additions and 0 deletions

View File

@ -8,10 +8,14 @@
package request package request
import ( import (
"bytes"
"errors" "errors"
"io/ioutil"
"net/http" "net/http"
"strings" "strings"
"git.zhangdeman.cn/zhangdeman/gin/define"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -29,6 +33,11 @@ type form struct {
// //
// Date : 00:34 2022/7/3 // Date : 00:34 2022/7/3
func (f *form) Parse(ctx *gin.Context, receiver interface{}) error { func (f *form) Parse(ctx *gin.Context, receiver interface{}) error {
requestBody, _ := ioutil.ReadAll(ctx.Request.Body)
// 请求信息写入上下文
ctx.Set(define.RecordRequestDataField, string(requestBody))
// 因为请求体被读一遍之后就没了,重新赋值 requestBody
ctx.Request.Body = ioutil.NopCloser(bytes.NewReader(requestBody))
method := strings.ToUpper(ctx.Request.Method) method := strings.ToUpper(ctx.Request.Method)
if method == http.MethodGet || if method == http.MethodGet ||
method == http.MethodPatch || method == http.MethodPatch ||