Skip to content

Responding directly in middleware #2578

Answered by aldas
SKFrozenCloud asked this question in Q&A
Discussion options

You must be logged in to vote

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.
  • usually when middleware/handler returns error the response has not been committed. So anything up in unwinding call chain can decide to "consume" the error and respond.

Example of one rare place where Commited is checked

echo/echo.go

Line 421 in 60fc2fb

if c.Response().Committed {

I hope this helps

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@SKFrozenCloud
Comment options

Answer selected by SKFrozenCloud
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants