英文:
GoREST endpoint path
问题
我正在使用Go编写一个Web服务,我想要的URL格式是:
http://example.com/WEB/service.wfs?param1=2¶m2=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包。
在其中有一个关于你想要做的事情的示例,大约在一半的位置。
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.
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论