Hi, I tried looking around existing issues but I could not see anything related. So, I would like to know if there is a way to support optional trailing slash at the end of a route.

For example:

r.POST("/users/?", ...)

Right now I have to use two posts:

r.POST("/users", ...)
r.POST("/users/", ...)

Comment From: nazwa

Hi @alansouzati

You can use r.RedirectTrailingSlash() to unify it. Might not work as intended on POST requests due to browser redirect conventions.

Comment From: alansouzati

yeah, i would definitely like to avoid a redirect response.

Comment From: nazwa

Just a thought - you might be able to hack it by creating a group at the desired endpoint and assigning your handler to the root of that group.

Comment From: alansouzati

do you have an example of that? We are already using a group. but it does not solve the problem.

users := rest.Group("/users")
users.POST("", ...)
users.POST("/", ...)

Where I would expect something like:

users := rest.Group("/users")
users.POST("/?", ...)

Comment From: nazwa

I was hoping this would do it:

users := rest.Group("/users")
users.POST("", ...)

I don't think there is any other way at the moment. For internal services, you can just be strict with yourself and decide on one or the other. For public ones, you probably should have nginx running in front of your go app anyway, and you can fix urls there.

Comment From: thinkerou

duplicated #279 and ref #1061

Comment From: kuchaguangjie

gin.Engine' s RedirectTrailingSlash is true by default, if it's created by gin.New().

Comment From: iTrooz

@thinkerou Are you sure this is a duplicate ? The linked PR to fix the issue (https://github.com/gin-gonic/gin/pull/1061) does not seem to address the issue, it even adds a test that enforces that /test/john/ and /test/john are treated differently

Comment From: charlie0129

@thinkerou it's not a duplicate, please reopen

Comment From: charlie0129

I added an option in Engine to solve this issue, allowing trailing-slash-insensitive matching. So no redirect response is generated. https://github.com/charlie0129/gin

@thinkerou If you think this feature is okay to be worked on, I can open a PR.