GoREST端点路径

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

GoREST endpoint path

问题

我正在使用Go编写一个Web服务,我想要的URL格式是:

http://example.com/WEB/service.wfs?param1=2&param2=test.....

我正在使用GoREST,我的端点URL是:

method:"GET" path:"/WEB/service.wfs?{param:string}" output:"string"

我的问题是它从来没有返回"param",但如果我使用以下端点,它会返回:

method:"GET" path:"/WEB/service.wfs/{param:string}" output:"string"

有没有办法处理"?"?

英文:

I'm writting a web service with Go and I'd like to have url like :

http://example.com/WEB/service.wfs?param1=2&param2=test.....

I'm using GoREST and my Endpoint url is :

method:"GET" path:"/WEB/service.wfs?{param:string}" output:"string"

My problem is that it never return the "param" but it does if I use the endpoint :

method:"GET" path:"/WEB/service.wfs/{param:string}" output:"string"

Is there a way to handle the "?" ?

答案1

得分: 1

你可以在gorest中这样做,尽管它不如gorest的首选机制那样好。

在你的端点定义中不要包含查询参数。

method:"GET" path:"/WEB/service.wfs" output:"string"

相反,你可以从注册的端点中获取上下文,并使用类似以下方式获取查询参数:

func (serv MyService) HelloWorld() (result string) {
    r := serv.Context.Request()
    u, _ := url.Parse(r.URL.String())
    q := u.Query()
    result = "Buono estente " + q["hi"][0]
    return
}
英文:

You can do this in gorest though it's not as nice as gorest's preferred mechanism.

Don't include your query parameters in your endpoint definition

method:"GET" path:"/WEB/service.wfs" output:"string"

Instead, you can get at the context from your registered end point and get the query parameters using something like

func (serv MyService) HelloWorld() (result string) {
    r := serv.Context.Request()
    u, _ := url.Parse(r.URL.String())
    q := u.Query()
    result = "Buono estente " + q["hi"][0]
    return
}

答案2

得分: 0

我已经查看了你正在使用的GoREST包,但没有找到任何可以实现这个的方法。

我一直使用gorillatoolkit的pat包。

gorillatoolkit

在其中有一个关于你想要做的事情的示例,大约在一半的位置。

category := req.URL.Query().Get("category")

这样你就可以通过键获取请求URL上的查询参数。

希望这可以帮到你。

英文:

I have had a look at the GoREST package you are using and can not see any way of doing this.

I have always used gorillatoolkit pat package.

gorillatoolkit

There is an example of what you want to do about half way down.

category := req.URL.Query().Get(":category")

This way you can get the query parameters on the request URL by the key.

Hope this helps.

huangapple
  • 本文由 发表于 2013年11月4日 17:50:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/19765128.html
匿名

发表评论

匿名网友

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

确定