3

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.

ning
  • 41
  • 1
  • 3
  • There is no easy way to do it, why you need that string? – Roman Kiselenko Oct 25 '18 at 06:33
  • 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 Oct 25 '18 at 14:23

1 Answers1

7

According to the documentation you can use FullPath().

router.GET("/user/:id", func(c *gin.Context) {
    c.FullPath() == "/user/:id" // true
})
Simon S.
  • 931
  • 1
  • 7
  • 21