یہ فصل پیش کرتی ہے کہ iris فریم ورک میں GET، POST اور دوسرے درخواست پیرامیٹر حاصل کرنے کا طریقہ، بسکت کو پڑھنا اور کوکیز میں لکھنا۔
راستے میں پیرامیٹر
func main() {
app := iris.Default()
// یہ ہینڈلر /user/john سے میل کھائے گا، لیکن /user/ یا /user پر نہیں
app.Get("/user/{name}", func(ctx iris.Context) {
name := ctx.Params().Get("name")
ctx.Writef("Hello %s", name)
})
// البتہ، یہ ہینڈلر /user/john/ اور /user/john/send کے ساتھ میل کھائے گا
// اگر کوئی دیگر راوٹر /user/john سے میل نہ کرے، تو یہ /user/john/ پر موجود ہوجائے گا
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)
})
// ہر ہم آہنگ درخواست کے لئے، متن کونٹیکسٹ راوٹر کی تعریف دوام میں رکھے گا
app.Post("/user/{name:string}/{action:path}", func(ctx iris.Context) {
ctx.GetCurrentRoute().Tmpl().Src == "/user/{name:string}/{action:path}" // true
})
app.Listen(":8080")
}
تیار پیرامیٹر اقسام:
پیرامیٹر قسم | Go ٹائپ | ویلیدیشن | ایکسیس ہیلپر فنکشن |
---|---|---|---|
:string |
string | کسی بھی مواد (ایکلے راستے کا حصہ) | Params().Get |
:uuid |
string | uuidv4 یا v1 (ایکلے راستے کا حصہ) | Params().Get |
:int |
int | -9223372036854775808 سے 9223372036854775807 (x64) یا -2147483648 سے 2147483647 (x32)، میزبان کی معماری پر منحصر ہے | Params().GetInt |
:int8 |
int8 | -128 سے 127 | Params().GetInt8 |
:int16 |
int16 | -32768 سے 32767 | Params().GetInt16 |
:int32 |
int32 | -2147483648 سے 2147483647 | Params().GetInt32 |
:int64 |
int64 | -9223372036854775808 سے 9223372036854775807 | Params().GetInt64 |
:uint |
uint | 0 سے 18446744073709551615 (x64) یا 0 سے 4294967295 (x32)، میزبان کی معماری پر منحصر ہے | Params().GetUint |
:uint8 |
uint8 | 0 سے 255 | Params().GetUint8 |
:uint16 |
uint16 | 0 سے 65535 | Params().GetUint16 |
:uint32 |
uint32 | 0 سے 4294967295 | Params().GetUint32 |
:uint64 |
uint64 | 0 سے 18446744073709551615 | Params().GetUint64 |
:bool |
bool | "1" یا "t" یا "T" یا "TRUE" یا "true" یا "True" یا "0" یا "f" یا "F" یا "FALSE" یا "false" یا "False" | Params().GetBool |
:alphabetical |
string | چھوٹے حروف یا بڑے حروف کے حروف | Params().Get |
:file |
string | چھوٹے حروف یا بڑے حروف کے حروف، اعداد، زیر خط (_), پھیپھڑا (-), نقطہ (.)، وغیرہ نہیں ہو سکتے، خالی جگہ یا دیگر غیر فعال فائل نامی خصوصی حروف نہیں ہوسکتے | Params().Get |
:path |
string | کسی بھی مواد، سلاش کی صورت میں الگ کیا جا سکتا ہے (راہ کے حصے)، لیکن راوٹر کا آخری حصہ ہونا چاہئے | Params().Get |
:mail |
string | ای میل پتا، ڈومین کا تصدیق نہیں ہوگا | Params().Get |
:email |
string | ای میل پتہ، ڈومین کی تصدیق ہوگی | Params().Get |
:date |
string | فارمیٹ yyyy/mm/dd، مثلاً /blog/{param:date} matches /blog/2022/04/21 | Params().GetTime اور Params().SimpleDate |
:weekday |
uint (0-6) یا string | اسٹرنگ کو وقت.ہفتہ دن ( "sunday" سے "monday" یا "Sunday" سے "Monday") فارمیٹ ہونا چاہئے، مثلاً /schedule/{param:weekday} matches /schedule/monday | Params().GetWeekday |
حاصل کرنا پارامیٹرز
func main() {
app := iris.Default()
// موجودہ کم سطح کے درخواست آبجیکٹ کا استعمال کرتے ہوئے کوئیری اسٹرنگ پیرامیٹرز کو تجزیہ کریں۔
// درخواست یو آر ایل میچ: /welcome?firstname=Jane&lastname=Doe
app.Get("/welcome", func(ctx iris.Context) {
firstname := ctx.URLParamDefault("firstname", "مہمان")
lastname := ctx.URLParam("lastname") // کرنے کی سریع راہنمائی ctx.Request().URL.Query().Get("lastname")
ctx.Writef("ہیلو %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", "ناشنامہ")
ctx.JSON(iris.Map{
"status": "شائع ہوگیا",
"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 /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) {
// کوکی پڑھیں
value := ctx.GetCookie("my_cookie")
if value == "" {
value = "نا تھا"
// کوکی بنائیں
ctx.SetCookieKV("my_cookie", value)
// یا: ctx.SetCookie(&http.Cookie{...})
// کوکی بنائیں
ctx.SetCookie("", "test", 3600, "/", "localhost", false, true)
}
ctx.Writef("کوکی کی قیمت: %s \n", value)
})
app.Listen(":8080")
}