Request - Complex Parameters - 图1tip

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:

ParameterVariable
k=m&k=nmap[k:n]
k1=m&k2=nmap[k1:m k2:n]
k[]=m&k[]=nmap[k:[m n]]
k[a][]=m&k[a][]=nmap[k:map[a:[m n]]]
k[a]=m&k[b]=nmap[k:map[a:m b:n]]
k[a][a]=m&k[a][b]=nmap[k:map[a:map[a:m b:n]]]
k=m&k[a]=nerror

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.

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/net/ghttp"
  5. )
  6. func main() {
  7. s := g.Server()
  8. s.BindHandler("/", func(r *ghttp.Request) {
  9. r.Response.Write(r.Get("name"))
  10. })
  11. s.SetPort(8199)
  12. s.Run()
  13. }

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.

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/net/ghttp"
  5. )
  6. func main() {
  7. s := g.Server()
  8. s.BindHandler("/", func(r *ghttp.Request) {
  9. r.Response.Write(r.Get("array"))
  10. })
  11. s.SetPort(8199)
  12. s.Run()
  13. }

After execution, when accessing http://127.0.0.1:8199/?array[]=john&array[]=smith, the returned value will be ["john","smith"].

Request - Complex Parameters - 图2warning

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.

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/net/ghttp"
  5. )
  6. func main() {
  7. s := g.Server()
  8. s.BindHandler("/", func(r *ghttp.Request) {
  9. r.Response.Write(r.Get("map"))
  10. })
  11. s.SetPort(8199)
  12. s.Run()
  13. }

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:

http://127.0.0.1:8199/?map[user1][id]=1&map[user1][name]=john&map[user2][id]=2&map[user2][name]=smith

The returned value will be {"user1":{"id":"1","name":"john"},"user2":{"id":"2","name":"smith"}}.