URL Path Parameters
Content-Type Requested: *
1
// Register the route...
2
app.Get("/{name}/{age:int}/{tail:path}", handler)
3
4
func handler(ctx iris.Context) {
5
params := ctx.Params()
6
7
name := params.Get("name")
8
age := params.GetIntDefault("age", 18)
9
tail := strings.Split(params.Get("tail"), "/")
10
}
Copied!

Bind

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.
1
type myParams struct {
2
Name string `param:"name"`
3
Age int `param:"age"`
4
Tail []string `param:"tail"`
5
}
Copied!
Now, the handler which reads the URL and binds to a "myParams" value.
1
func handler(ctx iris.Context) {
2
var p myParams
3
if err := ctx.ReadParams(&p); err != nil {
4
ctx.StopWithError(iris.StatusInternalServerError, err)
5
return
6
}
7
}
Copied!
Request
1
http://localhost:8080/kataras/27/iris/web/framework
Copied!
Result
1
myParams{
2
Name: "kataras",
3
Age: 27,
4
Tail: []string{"iris", "web", "framework"},
5
}
Copied!
Last modified 1yr ago
Export as PDF
Copy link
Contents
Bind