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

Middleware doesn't apply to Redirect #1985

Open
xdays opened this issue Jul 12, 2019 · 7 comments
Open

Middleware doesn't apply to Redirect #1985

xdays opened this issue Jul 12, 2019 · 7 comments

Comments

@xdays
Copy link

xdays commented Jul 12, 2019

Here's sample code:

package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

//CORSMiddleware ...
func CORSMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		c.Writer.Header().Set("Access-Control-Allow-Origin", "http://localhost")
		c.Writer.Header().Set("Access-Control-Max-Age", "86400")
		c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
		c.Writer.Header().Set("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Origin, Authorization, Accept, Client-Security-Token, Accept-Encoding, x-access-token")
		c.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Length")
		c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")

		if c.Request.Method == "OPTIONS" {
			c.AbortWithStatus(200)
		} else {
			c.Next()
		}
	}
}

func main() {
	r := gin.Default()
    r.Use(CORSMiddleware())
    g := r.Group("/users")

	g.GET("/", func(context *gin.Context) {
		context.JSON(http.StatusOK, gin.H{"hello": "world"})
	})

	r.Run(":8081")
}

Here's normal response with CORS headers.

curl -v localhost:8081/users/                                                       <aws:loop-staging>
*   Trying ::1:8081...
* TCP_NODELAY set
* Connected to localhost (::1) port 8081 (#0)
> GET /users/ HTTP/1.1
> Host: localhost:8081
> User-Agent: curl/7.65.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Access-Control-Allow-Credentials: true
< Access-Control-Allow-Headers: X-Requested-With, Content-Type, Origin, Authorization, Accept, Client-Security-Token, Accept-Encoding, x-access-token
< Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE, UPDATE
< Access-Control-Allow-Origin: http://localhost
< Access-Control-Expose-Headers: Content-Length
< Access-Control-Max-Age: 86400
< Content-Type: application/json; charset=utf-8
< Date: Fri, 12 Jul 2019 06:27:16 GMT
< Content-Length: 18
<
{"hello":"world"}
* Connection #0 to host localhost left intact

But my CORS header doesn't apear in the 301 redirect response

[14:27] ➜  ~ curl -v localhost:8081/users                                                        <aws:loop-staging>
*   Trying ::1:8081...
* TCP_NODELAY set
* Connected to localhost (::1) port 8081 (#0)
> GET /users HTTP/1.1
> Host: localhost:8081
> User-Agent: curl/7.65.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 301 Moved Permanently
< Content-Type: text/html; charset=utf-8
< Location: /users/
< Date: Fri, 12 Jul 2019 06:27:19 GMT
< Content-Length: 42
<
<a href="/users/">Moved Permanently</a>.

* Connection #0 to host localhost left intact
@Louis-Amas
Copy link

Same here did you find a fix?

@xdays
Copy link
Author

xdays commented May 2, 2020

My solution is not using api group for /users to avoid 301 redireciton.

@lolgolflol
Copy link

g.GET("", func(context *gin.Context) {
		context.JSON(http.StatusOK, gin.H{"hello": "world"})
	})

Try to use this code.

@tangx
Copy link

tangx commented Sep 27, 2021

still have this problem.

@tangx
Copy link

tangx commented Sep 27, 2021

and gin, for the present, call middleware after router finding.

#2413

		rg.GET("/", func(c *gin.Context) {
			_url := strings.TrimRight(c.Request.URL.Path, "/") + "?" + c.Request.URL.RawQuery
			c.Redirect(301, _url)
		})

it's ugly, but it works

@timqian
Copy link

timqian commented Feb 13, 2023

Any updates on this issue?

@yashvardhan-kukreja
Copy link

yashvardhan-kukreja commented Feb 29, 2024

Raised a fix here - #3858

You can directly start using it in your project by running this

go mod edit -replace="github.com/gin-gonic/gin=github.com/yashvardhan-kukreja/gin@issue-3857-onredirect-middleware"  && GOPROXY=direct go get -d ./...

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

6 participants