Gin: Setting headers in middleware chain

Created on 16 Dec 2014  路  2Comments  路  Source: gin-gonic/gin

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?

Most helpful comment

Yep, when you calling c.JSON etc, the headers were already written.

All 2 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mdnight picture mdnight  路  3Comments

olegsobchuk picture olegsobchuk  路  3Comments

iiinsomnia picture iiinsomnia  路  3Comments

xpbliss picture xpbliss  路  3Comments

mastrolinux picture mastrolinux  路  3Comments