2
votes

I have this code :

package main

import (
    "net/http"

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

func main() {
    r := gin.New()
    r.GET("/user/:id", func(c *gin.Context) {
        // How can I get the litteral string "/user/:id" here ?
        c.JSON(http.StatusOK, gin.H{"message": "received request"})
    })
}

Is there any way that I can retrieve inside the handler the litteral string /user/:id? If I use c.Request.Path it will give me the full output of the path like /user/10.

1
There is no easy way to do it, why you need that string? - Зелёный
There is no way, you would need to make some magic stuff, for example save all routes on a map, and compare that map with the current URL (you would need to replace params with their route value tho) - Raggaer

1 Answers

3
votes

According to the documentation you can use FullPath().

router.GET("/user/:id", func(c *gin.Context) {
    c.FullPath() == "/user/:id" // true
})