1234567891011121314151617181920212223242526272829303132 |
- package middleware
- import (
- "bytes"
- "git.sxidc.com/go-tools/api_binding/http_binding/binding_context"
- "git.sxidc.com/go-tools/api_binding/http_binding/response"
- "io"
- "net/http"
- )
- func ReadRequestBodyData() Func {
- return func(c *binding_context.Context) {
- respFunc := response.SendMsgResponse
- contentType := c.Request.Header.Get("Content-Type")
- if c.Request.Body != nil && contentType == "application/json" &&
- (c.Request.Method == http.MethodPost || c.Request.Method == http.MethodPut) {
- bodyData, err := io.ReadAll(c.Request.Body)
- if err != nil {
- respFunc(c, http.StatusBadRequest, nil, err)
- c.Abort()
- return
- }
- _ = c.Request.Body.Close()
- c.Request.Body = nil
- c.Request.Body = io.NopCloser(bytes.NewReader(bodyData))
- }
- c.Next()
- }
- }
|