github.com/gogf/gf@v1.16.9/net/ghttp/ghttp_z_example_get_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package ghttp_test
     8  
     9  import (
    10  	"fmt"
    11  
    12  	"github.com/gogf/gf/frame/g"
    13  )
    14  
    15  func ExampleClient_Get() {
    16  	url := "http://127.0.0.1:8999"
    17  	// Send with string parameter along with URL.
    18  	r1, err := g.Client().Get(url + "?id=10000&name=john")
    19  	if err != nil {
    20  		panic(err)
    21  	}
    22  	defer r1.Close()
    23  	fmt.Println(r1.ReadAllString())
    24  
    25  	// Send with string parameter in request body.
    26  	r2, err := g.Client().Get(url, "id=10000&name=john")
    27  	if err != nil {
    28  		panic(err)
    29  	}
    30  	defer r2.Close()
    31  	fmt.Println(r2.ReadAllString())
    32  
    33  	// Send with map parameter.
    34  	r3, err := g.Client().Get(url, g.Map{
    35  		"id":   10000,
    36  		"name": "john",
    37  	})
    38  	if err != nil {
    39  		panic(err)
    40  	}
    41  	defer r3.Close()
    42  	fmt.Println(r3.ReadAllString())
    43  
    44  	// Output:
    45  	// GET: query: 10000, john
    46  	// GET: query: 10000, john
    47  	// GET: query: 10000, john
    48  }
    49  
    50  func ExampleClient_GetBytes() {
    51  	url := "http://127.0.0.1:8999"
    52  	fmt.Println(string(g.Client().GetBytes(url, g.Map{
    53  		"id":   10000,
    54  		"name": "john",
    55  	})))
    56  
    57  	// Output:
    58  	// GET: query: 10000, john
    59  }
    60  
    61  func ExampleClient_GetContent() {
    62  	url := "http://127.0.0.1:8999"
    63  	fmt.Println(g.Client().GetContent(url, g.Map{
    64  		"id":   10000,
    65  		"name": "john",
    66  	}))
    67  
    68  	// Output:
    69  	// GET: query: 10000, john
    70  }
    71  
    72  func ExampleClient_GetVar() {
    73  	type User struct {
    74  		Id   int
    75  		Name string
    76  	}
    77  	var (
    78  		user *User
    79  		url  = "http://127.0.0.1:8999/var/json"
    80  	)
    81  	err := g.Client().GetVar(url).Scan(&user)
    82  	if err != nil {
    83  		panic(err)
    84  	}
    85  	fmt.Println(user)
    86  
    87  	// Output:
    88  	// &{1 john}
    89  }