This chapter introduces how to obtain GET, POST and other request parameters in the iris framework, including reading and writing cookies.
Parameters in the Path
func main() {
app := iris.Default()
// This handler will match /user/john, but not /user/ or /user
app.Get("/user/{name}", func(ctx iris.Context) {
name := ctx.Params().Get("name")
ctx.Writef("Hello %s", name)
})
// However, this handler will match /user/john/ and /user/john/send
// If no other router matches /user/john, it will redirect to /user/john/
app.Get("/user/{name}/{action:path}", func(ctx iris.Context) {
name := ctx.Params().Get("name")
action := ctx.Params().Get("action")
message := name + " is " + action
ctx.WriteString(message)
})
// For each matched request, the context will keep the router's definition
app.Post("/user/{name:string}/{action:path}", func(ctx iris.Context) {
ctx.GetCurrentRoute().Tmpl().Src == "/user/{name:string}/{action:path}" // true
})
app.Listen(":8080")
}
Built-in available parameter types:
Parameter Type | Go Type | Validation | Access Helper Function |
---|---|---|---|
:string |
string | Any content (single path segment) | Params().Get |
:uuid |
string | uuidv4 or v1 (single path segment) | Params().Get |
:int |
int | -9223372036854775808 to 9223372036854775807 (x64) or -2147483648 to 2147483647 (x32), depending on the host architecture | Params().GetInt |
:int8 |
int8 | -128 to 127 | Params().GetInt8 |
:int16 |
int16 | -32768 to 32767 | Params().GetInt16 |
:int32 |
int32 | -2147483648 to 2147483647 | Params().GetInt32 |
:int64 |
int64 | -9223372036854775808 to 9223372036854775807 | Params().GetInt64 |
:uint |
uint | 0 to 18446744073709551615 (x64) or 0 to 4294967295 (x32), depending on the host architecture | Params().GetUint |
:uint8 |
uint8 | 0 to 255 | Params().GetUint8 |
:uint16 |
uint16 | 0 to 65535 | Params().GetUint16 |
:uint32 |
uint32 | 0 to 4294967295 | Params().GetUint32 |
:uint64 |
uint64 | 0 to 18446744073709551615 | Params().GetUint64 |
:bool |
bool | "1" or "t" or "T" or "TRUE" or "true" or "True" or "0" or "f" or "F" or "FALSE" or "false" or "False" | Params().GetBool |
:alphabetical |
string | Lowercase or uppercase letters | Params().Get |
:file |
string | Lowercase or uppercase letters, numbers, underscore (_), hyphen (-), period (.), cannot contain spaces or other non-valid filename special characters | Params().Get |
:path |
string | Any content, can be separated by slashes (path segments), but should be the last part of the route path | Params().Get |
:mail |
string | Email address, domain not validated | Params().Get |
:email |
string | Email address, domain validated | Params().Get |
:date |
string | Format yyyy/mm/dd,for example /blog/{param:date} matches /blog/2022/04/21 | Params().GetTime and Params().SimpleDate |
:weekday |
uint (0-6) or string | String needs to be a time.Weekday constant ("sunday" to "monday" or "Sunday" to "Monday") format, for example /schedule/{param:weekday} matches /schedule/monday | Params().GetWeekday |
Getting Query Parameters
func main() {
app := iris.Default()
// Parse query string parameters using the existing low-level request object.
// Request URL match: /welcome?firstname=Jane&lastname=Doe
app.Get("/welcome", func(ctx iris.Context) {
firstname := ctx.URLParamDefault("firstname", "Guest")
lastname := ctx.URLParam("lastname") // Shortcut for ctx.Request().URL.Query().Get("lastname")
ctx.Writef("Hello %s %s", firstname, lastname)
})
app.Listen(":8080")
}
Getting Form Parameters
func main() {
app := iris.Default()
app.Post("/form_post", func(ctx iris.Context) {
message := ctx.PostValue("message")
nick := ctx.PostValueDefault("nick", "Anonymous")
ctx.JSON(iris.Map{
"status": "Published",
"message": message,
"nick": nick,
})
})
app.Listen(":8080")
}
Combined Example of Query + Form Parameters
POST /post?id=1234&page=1 HTTP/1.1
Content-Type: application/x-www-form-urlencoded
name=kataras&message=this_is_great
func main() {
app := iris.Default()
app.Post("/post", func(ctx iris.Context) {
id, err := ctx.URLParamInt("id", 0)
if err != nil {
ctx.StopWithError(iris.StatusBadRequest, err)
return
}
page := ctx.URLParamIntDefault("page", 0)
name := ctx.PostValue("name")
message := ctx.PostValue("message")
ctx.Writef("id: %d; page: %d; name: %s; message: %s", id, page, name, message)
})
app.Listen(":8080")
}
id: 1234; page: 1; name: kataras; message: this_is_great
Retrieving Query Parameters in a POST Request
POST /post?id=a&id=b&id=c&name=john&name=doe&name=kataras
Content-Type: application/x-www-form-urlencoded
func main() {
app := iris.Default()
app.Post("/post", func(ctx iris.Context) {
ids := ctx.URLParamSlice("id")
names, err := ctx.PostValues("name")
if err != nil {
ctx.StopWithError(iris.StatusBadRequest, err)
return
}
ctx.Writef("ids: %v; names: %v", ids, names)
})
app.Listen(":8080")
}
ids: [a b c], names: [john doe kataras]
Cookie Read/Write
import "github.com/kataras/iris/v12"
func main() {
app := iris.Default()
app.Get("/cookie", func(ctx iris.Context) {
// Read cookie
value := ctx.GetCookie("my_cookie")
if value == "" {
value = "NotSet"
// Create cookie
ctx.SetCookieKV("my_cookie", value)
// Alternatively: ctx.SetCookie(&http.Cookie{...})
// Create cookie
ctx.SetCookie("", "test", 3600, "/", "localhost", false, true)
}
ctx.Writef("Cookie value: %s \n", value)
})
app.Listen(":8080")
}