gzip.go 544 B

123456789101112131415161718192021222324252627
  1. package middleware
  2. import (
  3. "compress/gzip"
  4. "github.com/gin-gonic/gin"
  5. "io"
  6. "net/http"
  7. )
  8. func GzipDecodeMiddleware() gin.HandlerFunc {
  9. return func(c *gin.Context) {
  10. if c.GetHeader("Content-Encoding") == "gzip" {
  11. gzipReader, err := gzip.NewReader(c.Request.Body)
  12. if err != nil {
  13. c.AbortWithStatus(http.StatusBadRequest)
  14. return
  15. }
  16. defer gzipReader.Close()
  17. // Replace the request body with the decompressed data
  18. c.Request.Body = io.NopCloser(gzipReader)
  19. }
  20. // Continue processing the request
  21. c.Next()
  22. }
  23. }