Responding directly in middleware #2578
Answered
by
aldas
SKFrozenCloud
asked this question in
Q&A
|
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 |
Answered by
aldas
Jan 19, 2024
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 |
1 reply
Answer selected by
SKFrozenCloud
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
commitedmeans 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
Commitedis checkedecho/echo.go
Line 421 in 60fc2fb
I hope this helps