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

wildcard path param #2754

Closed
nnntlh opened this issue Jun 18, 2021 · 5 comments
Closed

wildcard path param #2754

nnntlh opened this issue Jun 18, 2021 · 5 comments

Comments

@nnntlh
Copy link

nnntlh commented Jun 18, 2021

  • With issues:
    • Use the search tool before opening a new issue.
    • Please provide source code and commit sha if you found a bug.
    • Review existing issues and provide feedback or react to them.

Description

  wildcard path param ,url not found

How to reproduce

package main

import (
	"fmt"

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

func handler(c *gin.Context) {
	fmt.Println(c.Params)
}

func main() {
	g := gin.Default()
	g.GET("/:resources/:name", handler)
	g.GET("/:resources", handler)
	g.GET("/namespaces/:namespace/:resources/:name", handler)
	g.GET("/namespaces/:namespace/:resources", handler)
	g.Run(":9090")
}

Actual result

$ curl http://localhost:9000/namespaces/apps
404 page not found

Environment

  • go version: 1.16
  • gin version (or commit ref): 1.7.2
  • operating system: Ubuntu 20.04.2 LTS
@nnntlh nnntlh closed this as completed Jun 18, 2021
@nnntlh nnntlh reopened this Jun 18, 2021
@nnntlh
Copy link
Author

nnntlh commented Jun 18, 2021

I have already checked #2706 ,But it didn't solve my problem, Please help me to check it. thx

@myml
Copy link

myml commented Jun 22, 2021

What you up to?
Maybe you can use curl http://localhost:9000/namespaces/a_name/apps

@appleboy
Copy link
Member

duplicated of #2762 ?

@rw-access
Copy link
Contributor

This looks like it might be different from #2762.

This particular case is actually not a bug but is working as designed. Basically each path element is checked, one at a time. Since /namespaces matched a full element in the path, the :resources wildcard will not match.

If you want, you can explicitly add a route for /namespaces/:namespace to hit the same handler as /:resources/:name

I think this ultimately comes down to a documentation thing. We don't have much documentation about how wildcards mix with exact paths, and should probably improveme it.

@appleboy
Copy link
Member

@nnntlh

package main

import (
	"fmt"

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

func handler(c *gin.Context) {
	fmt.Println(c.Params)
}

func main() {
	g := gin.Default()
	g.GET("/:resources/:name", handler)
	g.GET("/:resources", handler)
	g.GET("/namespaces/:namespace", handler)
	g.GET("/namespaces/:namespace/:resources/:name", handler)
	g.GET("/namespaces/:namespace/:resources", handler)
	g.Run(":9090")
}

base on the comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants