When an HTTP client initiates a request, it can customize the Cookie
content sent to the server. This feature is implemented using the SetCookie*
related methods.
Method list:
func (c *Client) SetCookie(key, value string) *Client
func (c *Client) SetCookieMap(m map[string]string) *Client
Let’s look at an example of a client customizing Cookie
.
Server
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.Cookie.Map())
})
s.SetPort(8199)
s.Run()
}
As this is an example, the server logic is straightforward, directly returning all received Cookie
parameters to the client.
Client
- Using the
SetCookie
method
package main
import (
"fmt"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
)
func main() {
c := g.Client()
c.SetCookie("name", "john")
c.SetCookie("score", "100")
if r, e := c.Get(gctx.New(), "http://127.0.0.1:8199/"); e != nil {
panic(e)
} else {
fmt.Println(r.ReadAllString())
}
}
Create a custom HTTP request client object using g.Client()
and set custom Cookie
using the c.SetCookie
method. Here, we set two example Cookie
parameters, one name
, and one score
.
- Using the
SetCookieMap
method
This method is simpler and allows setting Cookie
key-value pairs in bulk.
package main
import (
"fmt"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
)
func main() {
c := g.Client()
c.SetCookieMap(g.MapStrStr{
"name": "john",
"score": "100",
})
if r, e := c.Get(gctx.New(), "http://127.0.0.1:8199/"); e != nil {
panic(e)
} else {
fmt.Println(r.ReadAllString())
}
}
- Execution Result
After executing the client code, the terminal will print the server’s response as follows:
map[name:john score:100]
As you can see, the server has received the custom Cookie
parameters from the client.