Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't allow to override response header #3602

Open
ri8ika opened this issue May 10, 2023 · 2 comments
Open

Don't allow to override response header #3602

ri8ika opened this issue May 10, 2023 · 2 comments

Comments

@ri8ika
Copy link

ri8ika commented May 10, 2023

I have a specific situation where I don't want to let developer override response header if the header key matched.

So, whenever developer overrides response header supposedly like this:

router.GET("/:id", func(ctx *gin.Context) {
   ctx.Header("Content-Type", "application/json") // should panic or just do not set it.
   ctx.Header("X-Any-Other-Header", "other header") // Okay set it.
   // ...
}

I want to allow setting of any header is okay but setting of specific header should panic. In this case the header is "Content-Type". So, application should result in panic or just do not set it.

Is there a way to achieve this? I couldn't think of a solution because middleware runs first and this GET func runs later.

@ri8ika
Copy link
Author

ri8ika commented May 10, 2023

Found a way to solve this:

#2748 (comment)

@ri8ika ri8ika closed this as completed May 10, 2023
@ri8ika
Copy link
Author

ri8ika commented May 11, 2023

Re-opening this issue because code using afterMiddlewareWriter as in the linked issue returns gin default status code and not able to set my own. Thus, looking for better alternative to this.

@ri8ika ri8ika reopened this May 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant