Comments (6)
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.
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.
At present, i am using:
user:=make(map[string]string); user["name"]=ctx.URLParam("user[name]"); user["gender"]=ctx.URLParam("user[gender]");
from iris.
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.
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.
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)
- [HELP] How to proactively send websocket messages to the client in an MVC structure?
- [HELP] Multiple event when using native message
- Iris gRPC wrapper breaking HOT 1
- getBuiltinFuncs problem
- [BUG] template part Unable to load HOT 1
- [BUG] I used goroutine to affect the later HTTP before the service startup HOT 2
- [BUG] validation of binding is not effective
- [QUESTION] The session configuration provided by the iris framework is limited
- [FEATURE REQUEST] Get the data in the session through the session id
- [BUG] ctx.RemoveCookie need more special attribute(`domain` and `path`) value to remove cookies HOT 3
- slog is not in GOROOT HOT 1
- how to integrate SSE with mvc.
- [BUG] session recreation results in no session at all HOT 1
- How to integerate iris.FromStd with mvc.Application HOT 2
- [FEATURE REQUEST] SPA SubPath are not supported HOT 1
- ctx.Next() error
- Getting 403 Error on PUT | PATH | DELETE Request HOT 1
- [Question] Can not bind parameter of struct using ReadBody
- invalid version HOT 1
- [BUG]imported by a module that requires go 1.21
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from iris.