github.com/gogf/gf@v1.16.9/net/ghttp/ghttp_z_example_client_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_Header() {
    16  	var (
    17  		url    = "http://127.0.0.1:8999/header"
    18  		header = g.MapStrStr{
    19  			"Span-Id":  "0.1",
    20  			"Trace-Id": "123456789",
    21  		}
    22  	)
    23  	content := g.Client().Header(header).PostContent(url, g.Map{
    24  		"id":   10000,
    25  		"name": "john",
    26  	})
    27  	fmt.Println(content)
    28  
    29  	// Output:
    30  	// Span-Id: 0.1, Trace-Id: 123456789
    31  }
    32  
    33  func ExampleClient_HeaderRaw() {
    34  	var (
    35  		url       = "http://127.0.0.1:8999/header"
    36  		headerRaw = `
    37  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
    38  Span-Id: 0.1
    39  Trace-Id: 123456789
    40  `
    41  	)
    42  	content := g.Client().HeaderRaw(headerRaw).PostContent(url, g.Map{
    43  		"id":   10000,
    44  		"name": "john",
    45  	})
    46  	fmt.Println(content)
    47  
    48  	// Output:
    49  	// Span-Id: 0.1, Trace-Id: 123456789
    50  }
    51  
    52  func ExampleClient_Cookie() {
    53  	var (
    54  		url    = "http://127.0.0.1:8999/cookie"
    55  		cookie = g.MapStrStr{
    56  			"SessionId": "123",
    57  		}
    58  	)
    59  	content := g.Client().Cookie(cookie).PostContent(url, g.Map{
    60  		"id":   10000,
    61  		"name": "john",
    62  	})
    63  	fmt.Println(content)
    64  
    65  	// Output:
    66  	// SessionId: 123
    67  }
    68  
    69  func ExampleClient_ContentJson() {
    70  	var (
    71  		url     = "http://127.0.0.1:8999/json"
    72  		jsonStr = `{"id":10000,"name":"john"}`
    73  		jsonMap = g.Map{
    74  			"id":   10000,
    75  			"name": "john",
    76  		}
    77  	)
    78  	// Post using JSON string.
    79  	fmt.Println(g.Client().ContentJson().PostContent(url, jsonStr))
    80  	// Post using JSON map.
    81  	fmt.Println(g.Client().ContentJson().PostContent(url, jsonMap))
    82  
    83  	// Output:
    84  	// Content-Type: application/json, id: 10000
    85  	// Content-Type: application/json, id: 10000
    86  }