gf/net/ghttp/ghttp_z_example_get_test.go
2021-09-30 14:06:46 +08:00

99 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 (
"context"
"fmt"
"github.com/gogf/gf/frame/g"
)
func ExampleClient_Get() {
var (
ctx = context.Background()
url = "http://127.0.0.1:8999"
)
// Send with string parameter along with URL.
r1, err := g.Client().Get(ctx, url+"?id=10000&name=john")
if err != nil {
panic(err)
}
defer r1.Close()
fmt.Println(r1.ReadAllString())
// Send with string parameter in request body.
r2, err := g.Client().Get(ctx, url, "id=10000&name=john")
if err != nil {
panic(err)
}
defer r2.Close()
fmt.Println(r2.ReadAllString())
// Send with map parameter.
r3, err := g.Client().Get(ctx, url, g.Map{
"id": 10000,
"name": "john",
})
if err != nil {
panic(err)
}
defer r3.Close()
fmt.Println(r3.ReadAllString())
// Output:
// GET: query: 10000, john
// GET: query: 10000, john
// GET: query: 10000, john
}
func ExampleClient_GetBytes() {
var (
ctx = context.Background()
url = "http://127.0.0.1:8999"
)
fmt.Println(string(g.Client().GetBytes(ctx, url, g.Map{
"id": 10000,
"name": "john",
})))
// Output:
// GET: query: 10000, john
}
func ExampleClient_GetContent() {
url := "http://127.0.0.1:8999"
fmt.Println(g.Client().GetContent(ctx, url, g.Map{
"id": 10000,
"name": "john",
}))
// Output:
// GET: query: 10000, john
}
func ExampleClient_GetVar() {
type User struct {
Id int
Name string
}
var (
user *User
ctx = context.Background()
url = "http://127.0.0.1:8999/var/json"
)
err := g.Client().GetVar(ctx, url).Scan(&user)
if err != nil {
panic(err)
}
fmt.Println(user)
// Output:
// &{1 john}
}