-
Notifications
You must be signed in to change notification settings - Fork 4
/
test2_printserver.go
79 lines (51 loc) · 1.21 KB
/
test2_printserver.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
*
* Program to do a load test, and validate the response team of our api
*
* @package main
* @author @jeffotoni
* @size 28/07/2017
*
*/
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)
type Ping struct {
Msg string `json:"msg"`
}
func main() {
var ping = &Ping{}
url := "http://localhost:9001/ping"
fmt.Println("URL:>", url)
// var jsonStr = []byte(`{"msg":"hello"}`)
// bytes.NewBuffer(jsonStr)
for i := 0; i < 10; i++ {
time.Sleep(time.Duration(200) * time.Millisecond)
req, err := http.NewRequest("POST", url, nil)
req.Header.Set("X-Custom-Header", "valueHeader")
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
if resp.Status == "200 OK" {
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response Body:", string(body))
json.Unmarshal([]byte(string(body)), &ping)
fmt.Println("msg:", string(ping.Msg))
} else {
fmt.Println("response Status:", resp.Status)
fmt.Println("Error")
}
ping.Msg = ""
}
}