-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpUse.go
61 lines (52 loc) · 1.46 KB
/
httpUse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
)
type Header map[string][]string
type Person struct {
Name string `form:"name" json:"name,omitempty" binding:"required"`
Address string `form:"address" json:"address,omitempty" binding:"required"`
Birthday time.Time `form:"birthday" json:"birthday,omitempty" time_format:"2006-01-02" time_utc:"1" binding:"required"`
}
func main() {
// TestHttpPost()
TestHttpGet()
}
func TestHeader() {
header := Header{
"Content-Type": []string{"application/json"},
}
for k, v := range header {
fmt.Println(k, strings.Join(v, ""))
}
}
func TestHttpGet() {
resp, err := http.Get("http://xxx.lutaoact.com")
fmt.Printf("resp = %+v\n", resp)
fmt.Printf("err = %+v\n", err)
fmt.Println(buildHeaderString(resp.Header))
}
func TestHttpPost() {
p := &Person{"hello", "world", time.Now()}
pData, err := json.Marshal(p)
if err != nil {
panic(err)
}
res, err := http.Post("http://127.0.0.1:8080/testHttpPost", "application/json", bytes.NewBuffer(pData))
defer res.Body.Close()
data, err := ioutil.ReadAll(res.Body)
fmt.Printf("string(data) = %+v\n", string(data))
}
func buildHeaderString(header http.Header) string {
lines := make([]string, 0)
for k, v := range header {
lines = append(lines, fmt.Sprintf("%s: %s", k, strings.Join(v, "")))
}
return strings.Join(lines, "\n")
}