appleboy/gofight

How to set QueryParams on GET

jgoodall opened this issue · 7 comments

I see how to set form and json for POSTs, but how can I set query params on a get request?

To ask a less dumb question - is there a way to do it besides in the GET URL?

@jgoodall

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 I will implement new func SetQuery.

Yes, putting in URL works fine, just a little unreadable! Thanks, looking forward to the new SetQuery...

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

Great - thank you. Just noticed that SetQUERY and SetFORM probably should be camel case since they (unlike SetJSON) are not acronyms.

@jgoodall Good Point. I will change it later.