How to get all GET request query parameters into a structure in Go?

Hi I want to transform get query parameters into a structure in Go, for example I have this structure:

type Filter struct {
    Offset int64  `json:"offset"`
    Limit  int64  `json:"limit"`
    SortBy string `json:"sortby"`
    Asc    bool   `json:"asc"`

    //User specific filters
    Username   string `json:"username"`
    First_Name string `json:"first_name"`
    Last_Name  string `json:"last_name"`
    Status     string `json:"status"`
}

And I have the optional parameters that the user can specify when sending a get request which are Offset, Limit, SortBy, Asc, Username, First_Name, Last_Name, Status.

If those parameters were sent in the body then I would do this:

b, err := ioutil.ReadAll(r.Body)
if err != nil {

    log.WithFields(logFields).Errorf("Reading Body Message:failed:%v", err)

    return
}

var filter Filter
err = json.Unmarshal(b, &filter)

But I can't send body in a GET request so what is the solution instead of getting each parameter alone and then putting them into a structure?


Solution 1:

Using gorilla's schema package

The github.com/gorilla/schema package was invented exactly for this.

You can use struct tags to tell how to map URL parameters to struct fields, the schema package looks for the "schema" tag keys.

Using it:

import "github.com/gorilla/schema"

type Filter struct {
    Offset int64  `schema:"offset"`
    Limit  int64  `schema:"limit"`
    SortBy string `schema:"sortby"`
    Asc    bool   `schema:"asc"`

    //User specific filters
    Username   string `schema:"username"`
    First_Name string `schema:"first_name"`
    Last_Name  string `schema:"last_name"`
    Status     string `schema:"status"`
}

func MyHandler(w http.ResponseWriter, r *http.Request) {
    if err := r.ParseForm(); err != nil {
        // Handle error
    }

    filter := new(Filter)
    if err := schema.NewDecoder().Decode(filter, r.Form); err != nil {
        // Handle error
    }

    // Do something with filter
    fmt.Printf("%+v", filter)
}

Marshaling and unmarshaling using json

Note that the schema package will also try to convert parameter values to the type of the field.

If the struct would only contain fields of []string type (or you're willing to make that compromise), you can do that without the schema package.

Request.Form is a map, mapping from string to []string (as one parameter may be listed multiple times in the URL:

Form url.Values

And url.Values:

type Values map[string][]string

So for example if your Filter struct would look like this:

type Filter struct {
    Offset []string `json:"offset"`
    Limit  []string `json:"limit"`
    SortBy []string `json:"sortby"`
    // ..other fields
}

You could simply use the json package to marshal r.Form, then unmarshal it into your struct:

func MyHandler(w http.ResponseWriter, r *http.Request) {
    if err := r.ParseForm(); err != nil {
        // Handle error
    }
    data, err := json.Marshal(r.Form)
    if err != nil {
        // Handle error
    }
    filter := new(Fiter)
    if err = json.Unmarshal(data, filter); err != nil {
        // Handle error
    }
    fmt.Printf("%+v", filter)
}

This solution handles if multiple values are provided for the same parameter name. If you don't care about multiple values and you just want one, you first have to "transform" r.Form to a map with single string values instead of []string.

This is how it could look like:

type Filter struct {
    Offset string `json:"offset"`
    Limit  string `json:"limit"`
    SortBy string `json:"sortby"`
    // ..other fields
}

// Transformation from map[string][]string to map[string]string:
m := map[string]string{}
for k, v := range r.Form {
    m[k] = v[0]
}

And then you can marshal m and unmarshal into it into the Filter struct the same way.