Gin: when use context.Param() to get optional params from url, it will always return a slash

Created on 22 Feb 2018  路  3Comments  路  Source: gin-gonic/gin

router.GET("api/:fixed/*optional", func(context *gin.Context) {
t1 := context.Param("fixed")
t2 := context.Param("optional")
})

curl http://127.0.0.1:8000/api/user/123

t1 will be "user"
t2 will be "/123"

Is this a bug?
Thank you for any reply

Most helpful comment

router.GET("api/:fixed/:optional", func(context *gin.Context) {
  t1 := context.Param("fixed")
  t2 := context.Param("optional")
})

change *optional to :optional

All 3 comments

router.GET("api/:fixed/:optional", func(context *gin.Context) {
  t1 := context.Param("fixed")
  t2 := context.Param("optional")
})

change *optional to :optional

@appleboy Thank you for ur reply.
I do know use :optional can get the param value that without a slash, but why there should be a "/" when get an optional param? Is there any way to get optional param without that "/" ? Thank you for your kindness!

:optional is a better way

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mdsantosdev picture mdsantosdev  路  30Comments

nithinmohan picture nithinmohan  路  24Comments

cachafla picture cachafla  路  33Comments

monikaYZ picture monikaYZ  路  24Comments

elliotlings picture elliotlings  路  29Comments