Code Monkey home page Code Monkey logo

Comments (6)

kataras avatar kataras commented on May 14, 2024 2

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

from iris.

a7a2 avatar a7a2 commented on May 14, 2024 1

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

from iris.

vinhjaxt avatar vinhjaxt commented on May 14, 2024

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

from iris.

kataras avatar kataras commented on May 14, 2024

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

from iris.

vinhjaxt avatar vinhjaxt commented on May 14, 2024

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

}
`

from iris.

kataras avatar kataras commented on May 14, 2024

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

from iris.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.