body.go 786 B

1234567891011121314151617181920212223242526272829303132
  1. package middleware
  2. import (
  3. "bytes"
  4. "git.sxidc.com/go-tools/api_binding/http_binding/binding_context"
  5. "git.sxidc.com/go-tools/api_binding/http_binding/response"
  6. "io"
  7. "net/http"
  8. )
  9. func ReadRequestBodyData() Func {
  10. return func(c *binding_context.Context) {
  11. respFunc := response.SendMsgResponse
  12. contentType := c.Request.Header.Get("Content-Type")
  13. if c.Request.Body != nil && contentType == "application/json" &&
  14. (c.Request.Method == http.MethodPost || c.Request.Method == http.MethodPut) {
  15. bodyData, err := io.ReadAll(c.Request.Body)
  16. if err != nil {
  17. respFunc(c, http.StatusBadRequest, nil, err)
  18. c.Abort()
  19. return
  20. }
  21. _ = c.Request.Body.Close()
  22. c.Request.Body = nil
  23. c.Request.Body = io.NopCloser(bytes.NewReader(bodyData))
  24. }
  25. c.Next()
  26. }
  27. }