इस अध्याय में, आईरिस फ्रेमवर्क में 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}" // सत्य
    })
    
    app.Listen(":8080")
}

बिल्ट-इन उपलब्ध पैरामीटर प्रकार:

पैरामीटर प्रकार गो प्रकार सत्यापन पहुंच सहायक समारोह
: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 स्ट्रिंग एक वक्ति.Weekday स्थिर ("रविवार" से "सोमवार" या "Sunday" से "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; पृष्ठ: %d; नाम: %s; संदेश: %s", id, page, name, message)
    })
    app.Listen(":8080")
}
id: 1234; पृष्ठ: 1; नाम: kataras; संदेश: 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")
}