mirror of
https://github.com/kataras/iris.git
synced 2025-12-17 18:07:01 +00:00
add an example for #1659
This commit is contained in:
38
_examples/request-body/read-url/main.go
Normal file
38
_examples/request-body/read-url/main.go
Normal file
@@ -0,0 +1,38 @@
|
||||
// package main contains an example on how to use the ReadParams,
|
||||
// same way you can do the ReadQuery, ReadJSON, ReadProtobuf and e.t.c.
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
type myURL struct {
|
||||
Name string `url:"name"` // or `param:"name"`
|
||||
Age int `url:"age"` // >> >>
|
||||
Tail []string `url:"tail"` // >> >>
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := newApp()
|
||||
|
||||
// http://localhost:8080/iris/web/framework?name=kataras&age=27
|
||||
// myURL: main.myURL{Name:"kataras", Age:27, Tail:[]string{"iris", "web", "framework"}}
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/{tail:path}", func(ctx iris.Context) {
|
||||
var u myURL
|
||||
// ReadURL is a shortcut of ReadParams + ReadQuery.
|
||||
if err := ctx.ReadURL(&u); err != nil {
|
||||
ctx.StopWithError(iris.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Writef("myURL: %#v", u)
|
||||
})
|
||||
|
||||
return app
|
||||
}
|
||||
Reference in New Issue
Block a user