英文:
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.Values
(map[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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论