يقدم هذا الفصل كيفية الحصول على معلمات الطلب مثل GET و POST وغيرها في إطار العمل iris، بما في ذلك قراءة وكتابة الكوكيز.

المعلمات في المسار

func main() {
    app := iris.Default()

    // سيتطابق هذا المعالج مع /user/john، ولكن ليس /user/ أو /user
    app.Get("/user/{name}", func(ctx iris.Context) {
        name := ctx.Params().Get("name")
        ctx.Writef("مرحبًا %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}" // الصحيح
    })
    
    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} يتطابق مع /blog/2022/04/21 Params().GetTime و Params().SimpleDate
:weekday uint (0-6) أو string يجب أن يكون السلسلة ثابتة للوقت ("sunday" to "monday" أو "Sunday" to "Monday")، على سبيل المثال /schedule/{param:weekday} يتطابق مع /schedule/monday Params().GetWeekday

الحصول على معلمات الاستعلام

func main() {
    app := iris.Default()

    // تحليل معلمات سلسلة الاستعلام باستخدام كائن الطلب منخفض المستوى الحالي.
    // مطابقة عنوان URL: /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 = "NotSet"
            // إنشاء كوكي
            ctx.SetCookieKV("my_cookie", value)
            // بديلًا: ctx.SetCookie(&http.Cookie{...})
            // إنشاء كوكي
            ctx.SetCookie("", "test", 3600, "/", "localhost", false, true)
        }

        ctx.Writef("قيمة الكوكي: %s \n", value)
    })

    app.Listen(":8080")
}