Bu bölüm, iris framework'ünde GET, POST ve diğer istek parametrelerinin nasıl alınacağını, çerezlerin okunması ve yazılmasını tanıtır.
İsteğin Yolu Parametreleri
func main() {
app := iris.Default()
// Bu işleyici /user/john'a eşleşecektir, ancak /user/ veya /user'e eşleşmeyecektir
app.Get("/user/{name}", func(ctx iris.Context) {
name := ctx.Params().Get("name")
ctx.Writef("Merhaba %s", name)
})
// Bununla birlikte, bu işleyici /user/john/ ve /user/john/send ile eşleşecektir
// Diğer bir rota /user/john ile eşleşmezse, /user/john/ rotasına yönlendirilecektir
app.Get("/user/{name}/{action:path}", func(ctx iris.Context) {
name := ctx.Params().Get("name")
action := ctx.Params().Get("action")
message := name + " " + action
ctx.WriteString(message)
})
// Eşleşen her istek için, bağlam, rota tanımını koruyacaktır
app.Post("/user/{name:string}/{action:path}", func(ctx iris.Context) {
ctx.GetCurrentRoute().Tmpl().Src == "/user/{name:string}/{action:path}" // true
})
app.Listen(":8080")
}
Dahili mevcut parametre tipleri:
Parametre Tipi | Go Tipi | Doğrulama | Erişim Yardımcı Fonksiyonu |
---|---|---|---|
:string |
string | Herhangi bir içerik (tek yol segmenti) | Params().Get |
:uuid |
string | uuidv4 veya v1 (tek yol segmenti) | Params().Get |
:int |
int | -9223372036854775808'den 9223372036854775807'ye (x64) veya -2147483648'den 2147483647'ye (x32), konak mimarisine bağlı olarak değişir | Params().GetInt |
:int8 |
int8 | -128 ile 127 | Params().GetInt8 |
:int16 |
int16 | -32768 ile 32767 | Params().GetInt16 |
:int32 |
int32 | -2147483648 ile 2147483647 | Params().GetInt32 |
:int64 |
int64 | -9223372036854775808'den 9223372036854775807'ye | Params().GetInt64 |
:uint |
uint | 0 ile 18446744073709551615'e (x64) veya 0 ile 4294967295'e (x32), konak mimarisine bağlı olarak değişir | Params().GetUint |
:uint8 |
uint8 | 0 ile 255 arası | Params().GetUint8 |
:uint16 |
uint16 | 0 ile 65535 arası | Params().GetUint16 |
:uint32 |
uint32 | 0 ile 4294967295 arası | Params().GetUint32 |
:uint64 |
uint64 | 0 ile 18446744073709551615'e | Params().GetUint64 |
:bool |
bool | "1" veya "t" veya "T" veya "TRUE" veya "true" veya "True" veya "0" veya "f" veya "F" veya "FALSE" veya "false" veya "False" | Params().GetBool |
:alfabetik |
string | Küçük harf veya büyük harf harfler | Params().Get |
:file |
string | Küçük harf veya büyük harf harfler, sayılar, alt çizgi (_), kısa çizgi (-), nokta (.), boşluk veya diğer geçerli dosya adı özel karakterlerini içeremez | Params().Get |
:path |
string | Herhangi bir içerik, eğik çizgilerle ayrılabilir (yol segmentleri), ancak rota yolunun son kısmı olmalıdır | Params().Get |
:mail |
string | E-posta adresi, alan doğrulaması yapılmaz | Params().Get |
:email |
string | E-posta adresi, alan doğrulaması yapılır | Params().Get |
:date |
string | yyyy/ay/gün biçiminde, örneğin /blog/{param:date}, /blog/2022/04/21 ile eşleşir | Params().GetTime ve Params().SimpleDate |
:hafta_günü |
uint (0-6) veya string | Dize bir zaman.HaftaGünü sabit olmalıdır ("sunday" ile "monday" veya "Sunday" ile "Monday"), örneğin /schedule/{param:hafta_günü}, /schedule/monday ile eşleşir | Params().GetWeekday |
Sorgu Parametrelerini Alma
func main() {
app := iris.Default()
// Mevcut düşük seviyeli istek nesnesini kullanarak sorgu dizesi parametrelerini ayrıştırın.
// İstek URL eşleşmesi: /welcome?firstname=Jane&lastname=Doe
app.Get("/welcome", func(ctx iris.Context) {
firstname := ctx.URLParamDefault("firstname", "Misafir")
lastname := ctx.URLParam("lastname") // ctx.Request().URL.Query().Get("lastname") için kısayol
ctx.Writef("Merhaba %s %s", firstname, lastname)
})
app.Listen(":8080")
}
Form Parametrelerini Alma
func main() {
app := iris.Default()
app.Post("/form_post", func(ctx iris.Context) {
message := ctx.PostValue("message")
nick := ctx.PostValueDefault("nick", "Anonim")
ctx.JSON(iris.Map{
"durum": "Yayınlandı",
"mesaj": message,
"nick": nick,
})
})
app.Listen(":8080")
}
Sorgu + Form Parametrelerinin Birleşik Örneği
POST /post?id=1234&page=1 HTTP/1.1
Content-Type: application/x-www-form-urlencoded
name=kataras&message=bu_harika
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; sayfa: %d; isim: %s; mesaj: %s", id, sayfa, isim, mesaj)
})
app.Listen(":8080")
}
id: 1234; sayfa: 1; isim: kataras; mesaj: bu_harika
POST İsteğinde Sorgu Parametrelerini Alma
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; isimler: %v", ids, names)
})
app.Listen(":8080")
}
ids: [a b c], isimler: [john doe kataras]
Çerez Okuma/Yazma
import "github.com/kataras/iris/v12"
func main() {
app := iris.Default()
app.Get("/cookie", func(ctx iris.Context) {
// Çerez okuma
value := ctx.GetCookie("benim_cerezim")
if value == "" {
value = "Ayarlanmamış"
// Çerez oluşturma
ctx.SetCookieKV("benim_cerezim", value)
// Alternatif olarak: ctx.SetCookie(&http.Cookie{...})
// Çerez oluşturma
ctx.SetCookie("", "test", 3600, "/", "localhost", false, true)
}
ctx.Writef("Çerez değeri: %s \n", value)
})
app.Listen(":8080")
}