How to set QueryParams on GET
jgoodall opened this issue · 7 comments
jgoodall commented
I see how to set form and json for POSTs, but how can I set query params on a get request?
jgoodall commented
To ask a less dumb question - is there a way to do it besides in the GET URL?
appleboy commented
The first way is add Query string in the GET URL. Try the following example:
func TestBasicHelloWorld(t *testing.T) {
r := gofight.New()
r.GET("/?a=b&foo=bar").
// turn on the debug mode.
SetDebug(true).
Run(BasicEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, "Hello World", r.Body.String())
assert.Equal(t, http.StatusOK, r.Code)
})
}
The second way is implement new func SetQuery
.
jgoodall commented
Yes, putting in URL works fine, just a little unreadable! Thanks, looking forward to the new SetQuery
...
appleboy commented
@jgoodall Update to v1.0.2
and see the detail information on README.
https://github.com/appleboy/gofight#set-query-string
func TestQueryString(t *testing.T) {
r := gofight.New()
r.GET("/hello").
SetQUERY(gofight.H{
"a": "1",
"b": "2",
}).
Run(BasicEngine, func(r HTTPResponse, rq HTTPRequest) {
assert.Equal(t, http.StatusOK, r.Code)
})
}
jgoodall commented
Great - thank you. Just noticed that SetQUERY
and SetFORM
probably should be camel case since they (unlike SetJSON
) are not acronyms.