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

feat(backwards compatible): support for onredirect middleware #3858

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

yashvardhan-kukreja
Copy link

@yashvardhan-kukreja yashvardhan-kukreja commented Feb 29, 2024

Resolves #3857

Example:

package main

import (
	"github.com/gin-gonic/gin"
	"github.com/gin-contrib/cors"
)

func main() {
	g := gin.Default()
        g.OnRedirect(func(c *gin.Context) {
                     c.Writer.Header.Set("X-Foo", "Bar")
        })
	g.GET("/foo", func(c *gin.Context) {
		c.String(200, "Hello, World!"))
	})
	g.Run(":9000")
}

Calling this server at GET /foo/ responds with a 307 Response with Location: /foo and Response Header as X-Foo: Bar;

Just like this, even CORS-mitigation headers can be applied as well

A note on backwards compartibility

This PR complies with backwards compatibility by ensuring that the behaviour of a Gin with no explicitly attached redirectMethod-HandlerChain to the engine identically works like it worked in the past.

Only when a developer explicitly uses the .OnRedirect method on their GIn engine makes them leverage the functionalities offered by this PR

Signed-off-by: Yashvardhan Kukreja <yash.kukreja.98@gmail.com>
Signed-off-by: Yashvardhan Kukreja <yash.kukreja.98@gmail.com>
@yashvardhan-kukreja yashvardhan-kukreja changed the title feat: support for onredirect middleware feat(backwards compatible): support for onredirect middleware Feb 29, 2024
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

Successfully merging this pull request may close these issues.

Attach Response Headers (or middlewares) to redirected requests (CORS issues)
1 participant