tip
The complex parameters in this chapter are aimed at traditional Query
or Form
parameter transmission. As these methods are not elegantly managed and maintained for complex parameters, we recommend using JSON
data encoding for management and maintenance whenever encountering complex parameter transmission scenarios.
Complex Parameters
The ghttp.Request
object supports intelligent parameter type parsing (irrespective of request submission method and type). Below are examples of submitted parameters and the corresponding server-side variable types:
Parameter | Variable |
---|---|
k=m&k=n | map[k:n] |
k1=m&k2=n | map[k1:m k2:n] |
k[]=m&k[]=n | map[k:[m n]] |
k[a][]=m&k[a][]=n | map[k:map[a:[m n]]] |
k[a]=m&k[b]=n | map[k:map[a:m b:n]] |
k[a][a]=m&k[a][b]=n | map[k:map[a:map[a:m b:n]]] |
k=m&k[a]=n | error |
Parameters with the Same Name
Parameters with the same name are submitted in the format: k=v1&k=v2
, and subsequent variable values will overwrite the previous ones.
package main
import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
)
func main() {
s := g.Server()
s.BindHandler("/", func(r *ghttp.Request) {
r.Response.Write(r.Get("name"))
})
s.SetPort(8199)
s.Run()
}
After execution, when accessing http://127.0.0.1:8199/?name=john&name=smith, the returned value will be smith
.
Note: The HTTP Server
of the framework handles this similarly to PHP
, which is different from the Go
standard library. In the Go
standard library net/http
, the submitted parameters with the same name will be converted into a string array.
Array Parameters
Array parameters are submitted in the format: k[]=v1&k[]=v2
, using empty brackets []
to denote array parameters.
package main
import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
)
func main() {
s := g.Server()
s.BindHandler("/", func(r *ghttp.Request) {
r.Response.Write(r.Get("array"))
})
s.SetPort(8199)
s.Run()
}
After execution, when accessing http://127.0.0.1:8199/?array[]=john&array[]=smith, the returned value will be ["john","smith"]
.
warning
Note: If the passed parameters contain brackets and index numbers, the parameters will be converted into a map
according to the previously introduced rules for converting complex parameters. For example, array[0]=john&array[1]=smith
will be converted to map{"0":"john","1":"smith"}
.
Map
Parameters
Map
parameters are submitted in the format: k[a]=m&k[b]=n
, and support multi-level Map
, for example: k[a][a]=m&k[a][b]=n
.
package main
import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
)
func main() {
s := g.Server()
s.BindHandler("/", func(r *ghttp.Request) {
r.Response.Write(r.Get("map"))
})
s.SetPort(8199)
s.Run()
}
After execution, when accessing http://127.0.0.1:8199/?map[id]=1&map[name]=john, the returned value will be {"id":"1","name":"john"}
.
Let’s try a multi-level Map
. Manually visit the following address:
The returned value will be {"user1":{"id":"1","name":"john"},"user2":{"id":"2","name":"smith"}}
.