-
Notifications
You must be signed in to change notification settings - Fork 3
/
req_example.go
135 lines (121 loc) · 4.17 KB
/
req_example.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"fmt"
requests "github.com/alessiosavi/Requests"
"github.com/alessiosavi/Requests/datastructure"
"log"
"net/http"
"time"
)
func main() {
exampleGETRequest()
examplePOSTRequest()
exampleParallelRequest()
exampleBasicAuth()
}
func examplePOSTRequest() {
// Initialize request
var req *requests.Request
var err error
if req, err = requests.InitRequest("https://postman-echo.com", "POST", []byte("get?foo1=bar1&foo2=bar2"), false, true); err != nil {
fmt.Println("Error: ", err)
return
}
//Set custom headers directly in the method
if err = req.CreateHeaderList("Content-Type", "text/plain; charset=UTF-8", "Authorization", "Basic cG9zdG1hbjpwYXNzd29yZA=="); err != nil {
fmt.Println("Error: ", err)
return
}
// Or create a list of headers and use in the method
var headers []string
headers = append(headers, "Content-Type")
headers = append(headers, "text/plain; charset=UTF-8")
headers = append(headers, "Authorization")
headers = append(headers, "Basic cG9zdG1hbjpwYXNzd29yZA==")
if err = req.CreateHeaderList(headers...); err != nil {
fmt.Println("Error: ", err)
return
}
// Send the request and save to a properly structure
// GET, without BODY data (only used in POST), and enabling SSL certificate validation (skipTLS: false)
response := req.ExecuteRequest(&http.Client{})
fmt.Println("Headers: ", response.Headers)
fmt.Println("Status code: ", response.StatusCode)
fmt.Println("Time elapsed: ", response.Time)
fmt.Println("Error: ", response.Error)
fmt.Println("Body: ", string(response.Body))
// Or print them
fmt.Println(response.Dump())
}
func exampleGETRequest() {
// Initialize request
var req *requests.Request
req, err := requests.InitRequest("https://postman-echo.com/get?foo1=bar1&foo2=bar2", "GET", nil, false, true)
if err != nil {
fmt.Println("Error: ", err)
return
}
//Set custom headers directly in the method
err = req.CreateHeaderList("Content-Type", "text/plain; charset=UTF-8", "Authorization", "alessio:savi")
if err != nil {
fmt.Println("Error: ", err)
return
}
// Or create a list of headers and use in the method
var headers []string
headers = append(headers, "Content-Type")
headers = append(headers, "text/plain; charset=UTF-8")
headers = append(headers, "Authorization")
headers = append(headers, "alessio:savi")
err = req.CreateHeaderList(headers...)
if err != nil {
fmt.Println("Error: ", err)
return
}
// Send the request and save to a properly structure
// GET, without BODY data (only used in POST), and enabling SSL certificate validation (skipTLS: false)
response := req.ExecuteRequest(&http.Client{})
fmt.Println("Headers: ", response.Headers)
fmt.Println("Status code: ", response.StatusCode)
fmt.Println("Time elapsed: ", response.Time)
fmt.Println("Error: ", response.Error)
fmt.Println("Body: ", string(response.Body))
}
func exampleParallelRequest() {
start := time.Now()
// This array will contains the list of request
var reqs []requests.Request
// This array will contains the response from the given request
var response []datastructure.Response
// Set to run at max 100 request in parallel (use CPU count for best effort)
var N = 100
// Create the list of request
for i := 0; i < 20000; i++ {
// Run against the `server_example.py` present in this folder
req, err := requests.InitRequest("https://127.0.0.1:5000", "GET", nil, i%2 == 0, false) // Alternate cert validation
if err != nil {
log.Println("Skipping request [", i, "]. Error: ", err)
} else {
req.SetTimeout(10 * time.Second)
reqs = append(reqs, *req)
}
}
// Run the request in parallel
response = requests.ParallelRequest(reqs, N)
elapsed := time.Since(start)
for i := range response {
// Print the response
log.Println("Request [", i, "] -> ", response[i].Dump())
}
log.Printf("Requests took %s", elapsed)
}
func exampleBasicAuth() {
req, err := requests.InitRequest("https://postman-echo.com/basic-auth", "GET", []byte{}, false, false)
if err != nil {
fmt.Println("ERROR! ", err)
}
_ = req.CreateHeaderList("Accept", "application/json", "Accept-Language", "en_US", "Authorization", "postman:password")
client := &http.Client{}
resp := req.ExecuteRequest(client)
fmt.Println(resp.Dump())
}