The writermem privated member inside gin.Context make it hard to wrapper the default http.ResponseWriter instance.

Can I PR a new function to Wrapper/Replace the default http.ResponseWriter ?

Comment From: dengoswei

Why We need wrapper the default http.ResponseWriter:

  1. Check http.Response before it send back to client, ie: encode checking, ..;
  2. Replace the response if StatusCode != 200 in some case;

Comment From: syssam

Just simple write a middleware to handle this.

Comment From: dengoswei

Middleware can do all the thing with the request, before calling the next.

But after the next(), can't get the response result out of http.ResponseWriter or stop gin send back the response ?

Comment From: syssam

c.AbortWithStatusJSON reference https://github.com/gin-gonic/gin/blob/master/recovery.go

Comment From: dengoswei

thx. but: 1. AbortWithStatus doesn't work yet, I don't see anyway in the code check IsAbort; 2. AbortWithStatus only work if HandlerFunc panic before call any Write/WriteHeader;

Anyway, I dig little more into gin code, and decide that using decorator to work with the "save or replace the response before sending it to gin.Context".

Comment From: syssam

I tested it.It should be work.

Comment From: dengoswei

Don't fit the need, event it work..

Comment From: Mehrdad-Dadkhah

@dengoswei

Can help you this solution: https://stackoverflow.com/a/38548555/3994010 ?