github.com/gogf/gf@v1.16.9/net/ghttp/ghttp_z_example_post_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 "github.com/gogf/gf/frame/g" 12 ) 13 14 func ExampleClient_Post() { 15 url := "http://127.0.0.1:8999" 16 // Send with string parameter in request body. 17 r1, err := g.Client().Post(url, "id=10000&name=john") 18 if err != nil { 19 panic(err) 20 } 21 defer r1.Close() 22 fmt.Println(r1.ReadAllString()) 23 24 // Send with map parameter. 25 r2, err := g.Client().Post(url, g.Map{ 26 "id": 10000, 27 "name": "john", 28 }) 29 if err != nil { 30 panic(err) 31 } 32 defer r2.Close() 33 fmt.Println(r2.ReadAllString()) 34 35 // Output: 36 // POST: form: 10000, john 37 // POST: form: 10000, john 38 } 39 40 func ExampleClient_PostBytes() { 41 url := "http://127.0.0.1:8999" 42 fmt.Println(string(g.Client().PostBytes(url, g.Map{ 43 "id": 10000, 44 "name": "john", 45 }))) 46 47 // Output: 48 // POST: form: 10000, john 49 } 50 51 func ExampleClient_PostContent() { 52 url := "http://127.0.0.1:8999" 53 fmt.Println(g.Client().PostContent(url, g.Map{ 54 "id": 10000, 55 "name": "john", 56 })) 57 58 // Output: 59 // POST: form: 10000, john 60 } 61 62 func ExampleClient_PostVar() { 63 type User struct { 64 Id int 65 Name string 66 } 67 var ( 68 users []User 69 url = "http://127.0.0.1:8999/var/jsons" 70 ) 71 err := g.Client().PostVar(url).Scan(&users) 72 if err != nil { 73 panic(err) 74 } 75 fmt.Println(users) 76 77 // Output: 78 // [{1 john} {2 smith}] 79 }