2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2020-08-11 15:23:42 +08:00
|
|
|
//
|
|
|
|
// 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",
|
|
|
|
}
|
|
|
|
)
|
2021-09-30 14:06:46 +08:00
|
|
|
content := g.Client().Header(header).PostContent(ctx, url, g.Map{
|
2020-08-11 15:23:42 +08:00
|
|
|
"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
|
|
|
|
`
|
|
|
|
)
|
2021-09-30 14:06:46 +08:00
|
|
|
content := g.Client().HeaderRaw(headerRaw).PostContent(ctx, url, g.Map{
|
2020-08-11 15:23:42 +08:00
|
|
|
"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",
|
|
|
|
}
|
|
|
|
)
|
2021-09-30 14:06:46 +08:00
|
|
|
content := g.Client().Cookie(cookie).PostContent(ctx, url, g.Map{
|
2020-08-11 15:23:42 +08:00
|
|
|
"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.
|
2021-09-30 14:06:46 +08:00
|
|
|
fmt.Println(g.Client().ContentJson().PostContent(ctx, url, jsonStr))
|
2020-08-11 15:23:42 +08:00
|
|
|
// Post using JSON map.
|
2021-09-30 14:06:46 +08:00
|
|
|
fmt.Println(g.Client().ContentJson().PostContent(ctx, url, jsonMap))
|
2020-08-11 15:23:42 +08:00
|
|
|
|
|
|
|
// Output:
|
|
|
|
// Content-Type: application/json, id: 10000
|
|
|
|
// Content-Type: application/json, id: 10000
|
|
|
|
}
|