From bb3b7ae1774d74382b75e6480a7e2322cab2d109 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Sat, 20 Jul 2019 18:12:04 +0300 Subject: [PATCH] add url query params chapter --- Home.md | 1 + URL-query-parameters.md | 65 +++++++++++++++++++++++++++++++++++++++++ _Sidebar.md | 1 + 3 files changed, 67 insertions(+) create mode 100644 URL-query-parameters.md diff --git a/Home.md b/Home.md index 49c6731..ace5b0e 100644 --- a/Home.md +++ b/Home.md @@ -20,6 +20,7 @@ This wiki is the main source of documentation for **developers** working with (o * [[Override Context|Routing-override-context]] * [[API Versioning]] * [[Request Authentication]] +* [[URL Query Parameters]] * [[File Server]] * [[View]] * [[Dependency Injection|dependency-injection]] diff --git a/URL-query-parameters.md b/URL-query-parameters.md new file mode 100644 index 0000000..ef1cbb8 --- /dev/null +++ b/URL-query-parameters.md @@ -0,0 +1,65 @@ +The Iris Context has two methods that returns the `net/http` standard `http.ResponseWriter` and `http.Request` as we mention in the previous chapters. + +- `Context.Request()` +- `Context.ResponseWriter()` + +However except the unique Iris features and helpers that Iris Context offers, for easier development, we provide some wrappers of existing `net/http` capabilities as well. + +This is the full list of methods that can help you with URL query string. + +```go +// 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 +``` + +Query string parameters are parsed using the existing underlying request object. +The request responds to a url matching: _/welcome?firstname=Jane&lastname=Doe_. + +- `ctx.URLParam("lastname")` == `ctx.Request().URL.Query().Get("lastname")` + +Example Code: + +```go + app.Get("/welcome", func(ctx iris.Context) { + firstname := ctx.URLParamDefault("firstname", "Guest") + lastname := ctx.URLParam("lastname") + + ctx.Writef("Hello %s %s", firstname, lastname) + }) +``` diff --git a/_Sidebar.md b/_Sidebar.md index 0fa2aeb..87705c9 100644 --- a/_Sidebar.md +++ b/_Sidebar.md @@ -15,6 +15,7 @@ * [[Override Context|Routing-override-context]] * [[API Versioning]] * [[Request Authentication]] +* [[URL Query Parameters]] * [[File Server]] * [[View]] * [[Dependency Injection|dependency-injection]]