Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can i get an array value of context URLParam method? #701

Closed
vinhjaxt opened this issue Aug 2, 2017 · 6 comments
Closed

can i get an array value of context URLParam method? #701

vinhjaxt opened this issue Aug 2, 2017 · 6 comments

Comments

@vinhjaxt
Copy link

vinhjaxt commented Aug 2, 2017

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!

@vinhjaxt
Copy link
Author

vinhjaxt commented Aug 2, 2017

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

@kataras
Copy link
Owner

kataras commented Aug 2, 2017

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()

@kataras
Copy link
Owner

kataras commented Aug 2, 2017

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 :)

@a7a2
Copy link

a7a2 commented Aug 2, 2017

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

@vinhjaxt
Copy link
Author

vinhjaxt commented Aug 2, 2017

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

}
`

@kataras
Copy link
Owner

kataras commented May 4, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants