Im currently experimenting with various middleware handlers for gin and noticed
that setting response header after calling c.Next() does not work. Here's an example:
func BeforeAfterMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("X-Before", "Foo")
c.Next()
c.Writer.Header().Set("X-After", "Bar")
}
}
Only X-Before header is set when request is complete.
Is it a bug or am i doing something wrong?
Yep, when you calling c.JSON etc, the headers were already written.
https://github.com/gin-gonic/gin/issues/133 check this out and review your code for correct order of events, this is own nature of http.
Most helpful comment
Yep, when you calling
c.JSONetc, the headers were already written.