Dieses Kapitel führt ein, wie man GET, POST und andere Anfrageparameter im Iris-Framework erhält, einschließlich dem Lesen und Schreiben von Cookies.
Parameter im Pfad
func main() {
app := iris.Default()
// Dieser Handler passt zu /user/john, aber nicht zu /user/ oder /user
app.Get("/user/{name}", func(ctx iris.Context) {
name := ctx.Params().Get("name")
ctx.Writef("Hallo %s", name)
})
// Jedoch passt dieser Handler zu /user/john/ und /user/john/send.
// Wenn kein anderer Router zu /user/john passt, wird er zu /user/john/ weitergeleitet.
app.Get("/user/{name}/{action:path}", func(ctx iris.Context) {
name := ctx.Params().Get("name")
action := ctx.Params().Get("action")
message := name + " ist " + action
ctx.WriteString(message)
})
// Für jede übereinstimmende Anfrage behält der Kontext die Definition des Routers bei.
app.Post("/user/{name:string}/{action:path}", func(ctx iris.Context) {
ctx.GetCurrentRoute().Tmpl().Src == "/user/{name:string}/{action:path}" // true
})
app.Listen(":8080")
}
Verfügbare integrierte Parametertypen:
Parametertyp | Go-Typ | Validierung | Zugriffshilfsfunktion |
---|---|---|---|
:string |
string | Beliebige Inhalte (einzelnes Pfadsegment) | Params().Get |
:uuid |
string | uuidv4 or v1 (einzelnes Pfadsegment) | Params().Get |
:int |
int | -9223372036854775808 bis 9223372036854775807 (x64) oder -2147483648 bis 2147483647 (x32), abhängig von der Hostarchitektur | Params().GetInt |
:int8 |
int8 | -128 bis 127 | Params().GetInt8 |
:int16 |
int16 | -32768 bis 32767 | Params().GetInt16 |
:int32 |
int32 | -2147483648 bis 2147483647 | Params().GetInt32 |
:int64 |
int64 | -9223372036854775808 bis 9223372036854775807 | Params().GetInt64 |
:uint |
uint | 0 bis 18446744073709551615 (x64) oder 0 bis 4294967295 (x32), abhängig von der Hostarchitektur | Params().GetUint |
:uint8 |
uint8 | 0 bis 255 | Params().GetUint8 |
:uint16 |
uint16 | 0 bis 65535 | Params().GetUint16 |
:uint32 |
uint32 | 0 bis 4294967295 | Params().GetUint32 |
:uint64 |
uint64 | 0 bis 18446744073709551615 | Params().GetUint64 |
:bool |
bool | "1" oder "t" oder "T" oder "TRUE" oder "true" oder "True" oder "0" oder "f" oder "F" oder "FALSE" oder "false" oder "False" | Params().GetBool |
:alphabetical |
string | Klein- oder Großbuchstaben | Params().Get |
:file |
string | Klein- oder Großbuchstaben, Zahlen, Unterstrich (_), Bindestrich (-), Punkt (.), darf keine Leerzeichen oder andere nicht gültige Sonderzeichen für Dateinamen enthalten | Params().Get |
:path |
string | Beliebige Inhalte, kann durch Schrägstriche getrennt sein (Pfadtrennzeichen), sollte jedoch der letzte Teil des Routenpfads sein | Params().Get |
:mail |
string | E-Mail-Adresse, Domain nicht validiert | Params().Get |
:email |
string | E-Mail-Adresse, Domäne validiert | Params().Get |
:date |
string | Format JJJJ/MM/TT, zum Beispiel passt /blog/{param:date} zu /blog/2022/04/21 | Params().GetTime und Params().SimpleDate |
:weekday |
uint (0-6) oder string | String muss eine time.Weekday-Konstante ("sunday" bis "monday" oder "Sunday" bis "Monday") sein, z. B. passt /schedule/{param:weekday} zu /schedule/monday | Params().GetWeekday |
func main() {
app := iris.Default()
// Analyse der Abfragezeichenfolgenparameter unter Verwendung des vorhandenen Low-Level-Request-Objekts.
// Anfrage-URL-Übereinstimmung: /welcome?firstname=Jane&lastname=Doe
app.Get("/welcome", func(ctx iris.Context) {
firstname := ctx.URLParamDefault("firstname", "Gast")
lastname := ctx.URLParam("lastname") // Verkürzung für ctx.Request().URL.Query().Get("lastname")
ctx.Writef("Hallo %s %s", firstname, lastname)
})
app.Listen(":8080")
}
func main() {
app := iris.Default()
app.Post("/form_post", func(ctx iris.Context) {
message := ctx.PostValue("message")
nick := ctx.PostValueDefault("nick", "Anonym")
ctx.JSON(iris.Map{
"status": "Veröffentlicht",
"message": message,
"nick": nick,
})
})
app.Listen(":8080")
}
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
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]
import "github.com/kataras/iris/v12"
func main() {
app := iris.Default()
app.Get("/cookie", func(ctx iris.Context) {
// Cookie lesen
value := ctx.GetCookie("my_cookie")
if value == "" {
value = "Nicht festgelegt"
// Cookie erstellen
ctx.SetCookieKV("my_cookie", value)
// Alternativ: ctx.SetCookie(&http.Cookie{...})
// Cookie erstellen
ctx.SetCookie("", "test", 3600, "/", "localhost", false, true)
}
ctx.Writef("Cookie-Wert: %s \n", value)
})
app.Listen(":8080")
}