URL Path Parameters
Content-Type Requested: *
// Register the route...
app.Get("/{name}/{age:int}/{tail:path}", handler)
func handler(ctx iris.Context) {
params := ctx.Params()
name := params.Get("name")
age := params.GetIntDefault("age", 18)
tail := strings.Split(params.Get("tail"), "/")
}
The
Context.ReadParams(ptr interface{}) error
is the method which binds URL Dynamic Path Parameters from client request to a custom value.Let's create our Go structure.
type myParams struct {
Name string `param:"name"`
Age int `param:"age"`
Tail []string `param:"tail"`
}
Now, the handler which reads the URL and binds to a "myParams" value.
func handler(ctx iris.Context) {
var p myParams
if err := ctx.ReadParams(&p); err != nil {
ctx.StopWithError(iris.StatusInternalServerError, err)
return
}
}
Request
http://localhost:8080/kataras/27/iris/web/framework
Result
myParams{
Name: "kataras",
Age: 27,
Tail: []string{"iris", "web", "framework"},
}
Last modified 6mo ago