I have a specific situation where I don't want to let developer override response header if the header key matched.
So, whenever developer overrides response header supposedly like this:
router.GET("/:id", func(ctx *gin.Context) {
ctx.Header("Content-Type", "application/json") // should panic or just do not set it.
ctx.Header("X-Any-Other-Header", "other header") // Okay set it.
// ...
}
I want to allow setting of any header is okay but setting of specific header should panic. In this case the header is "Content-Type". So, application should result in panic or just do not set it.
Is there a way to achieve this? I couldn't think of a solution because middleware runs first and this GET
func runs later.
Comment From: ri8ika
Found a way to solve this:
https://github.com/gin-gonic/gin/issues/2748#issuecomment-1485713004
Comment From: ri8ika
Re-opening this issue because code using afterMiddlewareWriter
as in the linked issue returns gin default status code and not able to set my own. Thus, looking for better alternative to this.