2

How to add default header to response returned in Gin golang? I wanted to add Cache-Control: public, max-age=604800, immutable to the every response that I return.

Madiyor
  • 761
  • 9
  • 23

1 Answers1

4

I have solved the above question with following code

r := gin.New()

r.Use(func() gin.HandlerFunc {
        return func(c *gin.Context) {
            c.Writer.Header().Set("Cache-Control", "public, max-age=604800, immutable")
        }
    }()
)

However, it is not really good idea to add the given header to every request. It is better to add the header to cdn or static items. It can be achieved with NGINX or Traefik.

Madiyor
  • 761
  • 9
  • 23
  • I have used it with S3 storage and it is possible to add it there. It is not good place to add this header in gin golang – Madiyor Dec 03 '21 at 14:52