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

A routing bug? #3519

Open
zituocn opened this issue Mar 3, 2023 · 3 comments
Open

A routing bug? #3519

zituocn opened this issue Mar 3, 2023 · 3 comments

Comments

@zituocn
Copy link

zituocn commented Mar 3, 2023

Description

code:

package main

import (
	"net/http"
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	r.GET("/read_:id.htm", func(c *gin.Context) {
		id := c.Param("id")
		c.JSON(http.StatusOK, gin.H{
			"message": "pong",
			"id":      id,
		})
	})
	r.Run()
}

can match the following requests:

  1. /read_1
  2. /read_1.htm
  3. /read_1.html
  4. /read_1.aaaaaaaa
  5. ......

How to do a unique match?

Environment

  • go version: go1.18.10
  • gin version (or commit ref): v1.8.2
  • operating system:
@Cookiery
Copy link
Contributor

Cookiery commented Mar 14, 2023

Strange usage, maybe you can write id := c.Param("id.htm")
Or you can control your route in middleware or function.

@zituocn
Copy link
Author

zituocn commented Mar 15, 2023

Routes should have unique properties

@pscheid92
Copy link

pscheid92 commented May 4, 2023

The router's properties are pretty unique. It just is not the properties we expect here 🙈
According to Gin's radix-tree router, a wild card starts with : and ends at the next / or at the end of the given string. In your example, the wild card is id.htm and not id.


I guess you expected the router to pars something like this:

  • prefix: /read_
  • wildcard: :id
  • postfix: .htm

But as the wildcard ends with the next / or end of the string. So it actually is:

  • prefix: /read_
  • wildcard: :id.htm

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

3 participants