【问题标题】:Adding query Parameters to Go Json Rest向 Go Json Rest 添加查询参数
【发布时间】:2015-04-22 12:57:52
【问题描述】:

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

//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)
}

我知道 r.PathParams 包含 url 参数,但我似乎无法找到如何通过“?”查询参数。在网址中。

【问题讨论】:

    标签: json rest go query-parameters json-api


    【解决方案1】:

    鉴于 go-json-rest 是net/http 之上的一个薄包装,你看过that package's documentation 吗?具体来说,the Request object 有一个字段Form,其中包含查询字符串值的解析映射以及POST 数据,您可以将其作为url.Values (map[string][]string) 访问,或者特别从@ 检索一个987654328@.

    【讨论】:

    • 是的,几小时后我找到了。它被隐藏在该参数中。 r.URL.Query()。我很新,我很难识别源代码中的接口。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2010-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    相关资源
    最近更新 更多