Responding directly in middleware #2578
-
I know you are not suppose to respond from a middleware but I want to know why and what can go wrong with that. For example, the func serveFile(c echo.Context, file http.File, info os.FileInfo) error {
http.ServeContent(c.Response(), c.Request(), info.Name(), info.ModTime(), file)
return nil
}
But all the other functions such as func (c *context) Blob(code int, contentType string, b []byte) (err error) {
c.writeContentType(contentType)
c.response.WriteHeader(code)
_, err = c.response.Write(b)
return
} My question is: Can I return a response in a middleware with |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You can respond from middleware but make sure to return from current middleware so Note:
Example of one rare place where Line 421 in 60fc2fb I hope this helps |
Beta Was this translation helpful? Give feedback.
You can respond from middleware but make sure to return from current middleware so
next(c)
will not try to write already "commited" response.Note:
commited
means that something already send the response (status+headers+body) to the client and because of how HTTP works we can not send headers/status anymore.Example of one rare place where
Commited
is checkedecho/echo.go
Line 421 in 60fc2fb
I hope this helps