I know the response header cant be writen again after it has been writen because of the http limit. So cant write header after c.Next() in a middleware,but sometimes it relly is needed,can gin support the way to solve this? i have a suggest, c.Next(func(c *gin.Context){ //some code here will execute before write response in handler }) i do not know whether it is feasible

Comment From: ciys

can anybody help me?

Comment From: ammario

The only way for headers to be written after the body is declared is by holding the body in memory until all middlewares are done executing.

This would have considerable performance implications.

Comment From: ciys

why cant support the callback mechanism?

Comment From: dibrinsofor

@ammario would you mind sharing a sample?

I want to rewrite the response upon a failure in the middleware but keep getting the invalid WriteHeader code 0 error

Comment From: ammario

@dibrinsofor it's been a few years since I've used Gin so I can't tell you from memory, but try to create a fake http.ResponseWriter high up in your middleware stack that stores the body in a bytes.Buffer instead of writing directly back to the client. After c.Next() in that middle, check for an error (maybe stored in the context) and then only reply the buffered body if err == nil.