Golang - Iris Web Framework #14: URL Query Parameters

18 tháng 02, 2020 - 1420 lượt xem

Iris Context có hai phương thức trả về giá trị net/http tiêu chuẩn http.ResponseWriter và http.Request như chúng ta đã đề cập ở các phần trước.

  • Context.Request()
  • Context.ResponseWriter()

Tuy nhiên ngoại trừ các tính năng và trợ giúp độc đáo của Iris mà Iris cung cấp, để phát triển dễ dàng hơn, Iris cung cấp một số trình bao bọc của net/http hiện có.

Đây là danh sách đầy đủ các phương thức có thể giúp bạn khi làm việc với chuỗi truy vấn URL.

// URLParam returns true if the url parameter exists, otherwise false.
URLParamExists(name string) bool
// URLParamDefault returns the get parameter from a request,
// if not found then "def" is returned.
URLParamDefault(name string, def string) string
// URLParam returns the get parameter from a request, if any.
URLParam(name string) string
// URLParamTrim returns the url query parameter with
// trailing white spaces removed from a request.
URLParamTrim(name string) string
// URLParamTrim returns the escaped url query parameter from a request.
URLParamEscape(name string) string
// URLParamInt returns the url query parameter as int value from a request,
// returns -1 and an error if parse failed.
URLParamInt(name string) (int, error)
// URLParamIntDefault returns the url query parameter as int value from a request,
// if not found or parse failed then "def" is returned.
URLParamIntDefault(name string, def int) int
// URLParamInt32Default returns the url query parameter as int32 value from a request,
// if not found or parse failed then "def" is returned.
URLParamInt32Default(name string, def int32) int32
// URLParamInt64 returns the url query parameter as int64 value from a request,
// returns -1 and an error if parse failed.
URLParamInt64(name string) (int64, error)
// URLParamInt64Default returns the url query parameter as int64 value from a request,
// if not found or parse failed then "def" is returned.
URLParamInt64Default(name string, def int64) int64
// URLParamFloat64 returns the url query parameter as float64 value from a request,
// returns -1 and an error if parse failed.
URLParamFloat64(name string) (float64, error)
// URLParamFloat64Default returns the url query parameter as float64 value from a request,
// if not found or parse failed then "def" is returned.
URLParamFloat64Default(name string, def float64) float64
// URLParamBool returns the url query parameter as boolean value from a request,
// returns an error if parse failed or not found.
URLParamBool(name string) (bool, error)
// URLParams returns a map of GET query parameters separated by comma if more than one
// it returns an empty map if nothing found.
URLParams() map[string]string

Các tham số chuỗi truy vấn được phân tích cú pháp bằng cách sử dụng đối tượng yêu cầu cơ bản hiện có. Yêu cầu phản hồi với: /welcome?firstname=Jane&lastname=Doe.

  • ctx.URLParam("lastname") == ctx.Request().URL.Query().Get("lastname")

Ví dụ:

    app.Get("/welcome", func(ctx iris.Context) {
        firstname := ctx.URLParamDefault("firstname", "Guest")
        lastname := ctx.URLParam("lastname") 

        ctx.Writef("Hello %s %s", firstname, lastname)
    })

Phần tiếp theo chúng ta sẽ tìm hiểu về Forms.

 

Bình luận

avatar
Nguyễn Hàn Duy 2020-02-18 08:07:39.848401 +0000 UTC
Cảm ơn tác giả
Avatar
avatar
Lưu Thế Vinh 2020-02-18 08:11:09.151945 +0000 UTC
Tác giả cám ơn.
Avatar
* Vui lòng trước khi bình luận.
Ảnh đại diện
  0 Thích
0