Open melugoyal opened 1 year ago
To my understanding, the implementation of c.Writer
is responseWriter
, the responseWriter.Write will call the WriteHeader
first.
Right, but i am using a custom ResponseWriter with Gin (injected into the context), and when doing so, expect Gin to call WriteHeader before calling Write, as is documented in the interface godoc:
If WriteHeader is not called explicitly, the first call to Write will trigger an implicit WriteHeader(http.StatusOK). Thus explicit calls to WriteHeader are mainly used to send error codes
I get it. It looks like it will break some existing conventions.
Description
Gin should call
c.Writer.WriteHeader(code)
before callingc.Writer.Write(defaultMessage)
when handling errors here: https://github.com/gin-gonic/gin/blob/b04917c53e310e3746ec90cd93106cda8c956211/gin.go#L658from the documentation of the Write() function in net/http's ResponseWriter interface:
This is an issue when injecting a custom response writer into Gin that needs to know the status that was written before
Write
is called. Otherwise the custom response writer defaults to 200 OK per the documentation above, then Gin later tries writing a different header withWriteHeaderNow()
.Environment
master
as well:b04917c53
)