Adding query Parameters to Go Json Rest

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

Adding query Parameters to Go Json Rest

问题

我正在使用go-json-rest库。我试图在代码中识别查询参数,例如localhost:8080/reminders?hello=world,我想要访问{hello: world}。我有以下代码:

//在另一个函数中
&rest.Route{"GET", "/reminders", i.GetAllReminders},

func (i *Impl) GetAllReminders(w rest.ResponseWriter, r *rest.Request) {
reminders := []Reminder{}
i.DB.Find(&reminders)
w.WriteJson(&reminders)
}

我知道r.PathParams保存URL参数,但我似乎找不到如何获取URL中"?"后面的查询参数。

英文:

I am using the library go-json-rest. I'm trying to recognize queries parameters in the code for example localhost:8080/reminders?hello=world I want to access {hello: world} . I have the following code:

//in another function
&rest.Route{"GET", "/reminders", i.GetAllReminders},

func (i *Impl) GetAllReminders(w rest.ResponseWriter, r *rest.Request) {
	reminders := []Reminder{}
	i.DB.Find(&reminders)
	w.WriteJson(&reminders)
}

I know that r.PathParams holds the url parameters but I cannot seem to find how to the query parameters past the "?" in the url.

答案1

得分: 2

鉴于go-json-rest只是在net/http之上的一个薄包装,你是否查看过该包的文档?具体来说,Request对象有一个字段Form,其中包含解析后的查询字符串值和POST数据,你可以将其作为url.Valuesmap[string][]string)访问,或者从FormValue中获取特定的值。

英文:

Given that go-json-rest is a thin wrapper on top of net/http, have you looked at that package's documentation? Specifically, the Request object has a field Form that contains a parsed map of query string values as well as POST data, that you can access as a url.Values (map[string][]string), or retrieve one in particular from FormValue.

huangapple
  • 本文由 发表于 2015年2月21日 02:36:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/28635895.html
匿名

发表评论

匿名网友

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

确定