gf/net/ghttp/ghttp_z_example_client_test.go
2021-01-17 21:46:25 +08:00

87 lines
1.8 KiB
Go

// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package ghttp_test
import (
"fmt"
"github.com/gogf/gf/frame/g"
)
func ExampleClient_Header() {
var (
url = "http://127.0.0.1:8999/header"
header = g.MapStrStr{
"Span-Id": "0.1",
"Trace-Id": "123456789",
}
)
content := g.Client().Header(header).PostContent(url, g.Map{
"id": 10000,
"name": "john",
})
fmt.Println(content)
// Output:
// Span-Id: 0.1, Trace-Id: 123456789
}
func ExampleClient_HeaderRaw() {
var (
url = "http://127.0.0.1:8999/header"
headerRaw = `
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3950.0 Safari/537.36
Span-Id: 0.1
Trace-Id: 123456789
`
)
content := g.Client().HeaderRaw(headerRaw).PostContent(url, g.Map{
"id": 10000,
"name": "john",
})
fmt.Println(content)
// Output:
// Span-Id: 0.1, Trace-Id: 123456789
}
func ExampleClient_Cookie() {
var (
url = "http://127.0.0.1:8999/cookie"
cookie = g.MapStrStr{
"SessionId": "123",
}
)
content := g.Client().Cookie(cookie).PostContent(url, g.Map{
"id": 10000,
"name": "john",
})
fmt.Println(content)
// Output:
// SessionId: 123
}
func ExampleClient_ContentJson() {
var (
url = "http://127.0.0.1:8999/json"
jsonStr = `{"id":10000,"name":"john"}`
jsonMap = g.Map{
"id": 10000,
"name": "john",
}
)
// Post using JSON string.
fmt.Println(g.Client().ContentJson().PostContent(url, jsonStr))
// Post using JSON map.
fmt.Println(g.Client().ContentJson().PostContent(url, jsonMap))
// Output:
// Content-Type: application/json, id: 10000
// Content-Type: application/json, id: 10000
}