Iris: can i get an array value of context URLParam method?

Created on 2 Aug 2017  Â·  6Comments  Â·  Source: kataras/iris

Example: URI: /path?user[name]=Vinhjaxt&user[gender]=0
Can i get user array?
Any way like:
user:=ctx.URLParamGetArray("user").(map[string]interface{}); fmt.Println(user["name"].(string)) ?
Thank you!

question

Most helpful comment

However if you want to fill a struct from query arguments, that is possible with the current implementation:

// file: main.go
package main

import (
    "github.com/kataras/iris"
    "github.com/kataras/iris/context"
)

type Visitor struct {
    Username string
    Mail     string
        Data []string
        // if you use uncomment this you have to use &mydata=data...
    // Data     []string `form:"mydata"`
}

func main() {
    app := iris.New()
    app.Get("/query", func(ctx context.Context) {
        visitor := Visitor{}
        err := ctx.ReadForm(&visitor)
        if err != nil {
            ctx.StatusCode(iris.StatusInternalServerError)
            ctx.WriteString(err.Error())
        }

        ctx.Writef("Visitor: %#v", visitor)
    })

    app.Run(iris.Addr(":8080"))
}
go run main.go

Open: http://localhost:8080/query?Username=myusername&Mail=mymail&Data=mydataaaa

Result:

Visitor: main.Visitor{Username:"myusername", Mail:"mymail", Data:[]string{"mydataaaa"}}

Hope that answer your question :)

All 6 comments

At present, i am using:
user:=make(map[string]string); user["name"]=ctx.URLParam("user[name]"); user["gender"]=ctx.URLParam("user[gender]");

You could just get the slice of string (or url.Values to be more specific) as you used to with net/http.

As you know Iris is fully compatible with net/http, known these:

ctx.Request() // returns the *http.Request
ctx.ResponseWriter() // returns the http.ResponseWriter

We can get the url values by, simply, using ctx.Request().URL.Query()

However if you want to fill a struct from query arguments, that is possible with the current implementation:

// file: main.go
package main

import (
    "github.com/kataras/iris"
    "github.com/kataras/iris/context"
)

type Visitor struct {
    Username string
    Mail     string
        Data []string
        // if you use uncomment this you have to use &mydata=data...
    // Data     []string `form:"mydata"`
}

func main() {
    app := iris.New()
    app.Get("/query", func(ctx context.Context) {
        visitor := Visitor{}
        err := ctx.ReadForm(&visitor)
        if err != nil {
            ctx.StatusCode(iris.StatusInternalServerError)
            ctx.WriteString(err.Error())
        }

        ctx.Writef("Visitor: %#v", visitor)
    })

    app.Run(iris.Addr(":8080"))
}
go run main.go

Open: http://localhost:8080/query?Username=myusername&Mail=mymail&Data=mydataaaa

Result:

Visitor: main.Visitor{Username:"myusername", Mail:"mymail", Data:[]string{"mydataaaa"}}

Hope that answer your question :)

How to get array like this? How to type struct?
form data:
id:5
list[0][playId]:1
list[0][Money]:29.88
list[1][playId]:22
list[1][Money]:129.88
list[2][playId]:13
list[2][Money]:39.88

Read More info at:
https://github.com/monoculum/formam
`
// ReadForm binds the formObject with the form data
// it supports any kind of struct.
func (ctx *context) ReadForm(formObject interface{}) error {
values := ctx.FormValues()
if values == nil {
return errors.New("An empty form passed on ReadForm")
}

// or dec := formam.NewDecoder(&formam.DecoderOptions{TagName: "form"})
// somewhere at the app level. I did change the tagName to "form"
// inside its source code, so it's not needed for now.
return errReadBody.With(formam.Decode(values, formObject))

}
`

Update (2020) for anyone who is searching for that.

https://github.com/kataras/iris/blob/master/_examples/http_request/read-form/main.go#L11

A PostDataForm struct is missing the "form" tag? Try:

type PostDataForm struct {
    RouterId int `json:"router_id" form:"router_id"`
    RoleList []int `json:"role_list" form:"role_list"`
}

Example: https://github.com/kataras/iris/blob/master/_examples/http_request/read-form/main.go

Was this page helpful?
0 / 5 - 0 ratings