从请求中提取URL模式路径

huangapple go评论64阅读模式
英文:

go-restful extract URL pattern path from request

问题

我正在使用emicklei/go-restful框架处理REST API。我希望能够从请求中访问路由路径。也就是说,当我配置一个虚拟路由时,我希望能够访问到路由的信息,例如:

    ws := new(restful.WebService)
    ws.Path("/resources/names")
    ws.Route(ws.GET("/{name}").To(getName))
    restful.DefaultContainer.Add(ws)

我希望能够访问到路由的路径信息,即"/resources/names/{name}"。

通过调用req.Request.URL.Path可以获取到实际的URL,但这只会返回具体的URL,而不是通用的路由路径。

有什么建议吗?

英文:

I am using a the emicklei/go-restful framework to deal with rest API.
I wish to access the route path from the request. Meaning that when I configure a dummy route like this:

    ws := new(restful.WebService)
	ws.Path("/resources/names")
	ws.Route(ws.GET("/{name}").To(getName))
    restful.DefaultContainer.Add(ws)

I wish to access the information that the route was "/resources/names/{name}"

I can access the actual URL which is call by calling:

    req.Request.URL.Path

But this will return the specific URL, not the generic one.

Any suggestion?

答案1

得分: 0

经过更多的研究,我终于发现方法req.SelectedRoutePath()将返回预期的值。

英文:

After more research I finally found that the method req.SelectedRoutePath() will return expected value.

huangapple
  • 本文由 发表于 2022年1月11日 07:00:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/70659836.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定