gf/net/ghttp/ghttp_z_example_client_test.go

87 lines
1.9 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// 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(ctx, 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(ctx, 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(ctx, 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(ctx, url, jsonStr))
// Post using JSON map.
fmt.Println(g.Client().ContentJson().PostContent(ctx, url, jsonMap))
// Output:
// Content-Type: application/json, id: 10000
// Content-Type: application/json, id: 10000
}