บทนี้จะแนะนำวิธีการรับพารามิเตอร์ของ 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}" // true
    })
    
    app.Listen(":8080")
}

ประเภทพารามิเตอร์ที่มีอยู่แบบ built-in :

ประเภทพารามิเตอร์ ประเภท 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 สารเนา, สามารถแยกด้วยเส้นเท่าสาม (segment ของเส้นทาง), แต่ควรเป็นส่วนสุดท้ายของเส้นทางเท่านั้น 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" ถึง "monday" หรือ "Sunday" ถึง "Monday") รูปแบบเช่น /schedule/{param:weekday} ตรงกับ /schedule/monday Params().GetWeekday

การรับค่าพารามิเตอร์คิวรี

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

    // วิธีการแยกข้อมูลพารามิเตอร์จาก query string โดยใช้ออบเจกต์ของ request ที่ระดับต่ำที่มีอยู่แล้ว
    // URL ที่ถูกตรงตั้งค่า: /welcome?firstname=Jane&lastname=Doe
    app.Get("/welcome", func(ctx iris.Context) {
        firstname := ctx.URLParamDefault("firstname", "Guest")
        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")
}

ตัวอย่างการใช้ค่าพารามิเตอร์ทั้งคำสั่ง query และฟอร์มร่วมกัน

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")
}