body.go 902 B

1234567891011121314151617181920212223242526272829303132333435
  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. "git.sxidc.com/service-supports/fslog"
  7. "io"
  8. "net/http"
  9. )
  10. func ReadRequestBodyData() Func {
  11. return func(c *binding_context.Context) {
  12. respFunc := response.SendMsgResponse
  13. contentType := c.Request.Header.Get("Content-Type")
  14. if c.Request.Body != nil && contentType == "application/json" &&
  15. (c.Request.Method == http.MethodPost || c.Request.Method == http.MethodPut) {
  16. bodyData, err := io.ReadAll(c.Request.Body)
  17. if err != nil {
  18. respFunc(c, http.StatusBadRequest, nil, err)
  19. c.Abort()
  20. return
  21. }
  22. fslog.Info(c.Request.RequestURI + " Request Body\n" + string(bodyData))
  23. _ = c.Request.Body.Close()
  24. c.Request.Body = nil
  25. c.Request.Body = io.NopCloser(bytes.NewReader(bodyData))
  26. }
  27. c.Next()
  28. }
  29. }