Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
I have a Middleware which adds the xml.Header string to every response body.
As you can see in the Code Example provided this works flawlessly when the StatusCode is set to be 200.
If the status Code set to something else the middleware still returns 200 in the response status. The expected behaviour should be the Status code of the one set by c.XML.
I nailed down the Problem to the function:
func (w *responseWriter) WriteHeader(code int) {
if code > 0 && w.status != code {
if w.Written() {
debugPrint("[WARNING] Headers were already written. Wanted to override status code %d with %d", w.status, code)
return
}
w.status = code
}
}
I want to override the response header. I accedently set the ResponseHeader when writing to the response body befor calling c.Next().
Currently I don't see another solution to prepend the xml.Header-string to the body than my solution.
But the behaviour of the response status code is an unexpected side behaviour I am trying to solve.
Beacuse the Write will call w.WriteHeaderNow(), and will write the default status code 200. Then w.Written will return true, and not overwrite the http code.
Description
I have a Middleware which adds the xml.Header string to every response body. As you can see in the Code Example provided this works flawlessly when the StatusCode is set to be 200. If the status Code set to something else the middleware still returns 200 in the response status. The expected behaviour should be the Status code of the one set by c.XML.
I nailed down the Problem to the function:
I want to override the response header. I accedently set the ResponseHeader when writing to the response body befor calling c.Next(). Currently I don't see another solution to prepend the xml.Header-string to the body than my solution. But the behaviour of the response status code is an unexpected side behaviour I am trying to solve.
How to reproduce
Playground Link
Expectations
Actual result
Environment