-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.go
100 lines (77 loc) · 1.88 KB
/
base.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
package ghttp
import "log"
// GET sends a GET request to the specified URL and returns the HTTP response.
// If an error occurs during the request process, it will be logged and a nil response will be returned.
func GET(URL string) *HttpResponse {
r := NewHttp()
err := r.SetURL(URL)
if err != nil {
return nil
}
err = r.SetMethod("GET")
if err != nil {
return nil
}
response, err := r.Do()
if err != nil {
log.Fatal(err)
}
return response
}
// POST sends a POST request to the specified URL with the provided data and returns the HTTP response.
// If an error occurs during the request process, it will be logged and a nil response will be returned.
func POST(URL string, data string) *HttpResponse {
var err error
r := NewHttp()
err = r.SetURL(URL)
if err != nil {
log.Fatal(err)
}
err = r.SetMethod("POST")
if err != nil {
log.Fatal(err)
}
r.SetData(data)
r.SetContentType("application/x-www-form-urlencoded")
response, err := r.Do()
if err != nil {
log.Fatal(err)
}
return response
}
// PUT sends a PUT request to the specified URL and returns the HTTP response.
// If an error occurs during the request process, it will be logged and a nil response will be returned.
func PUT(URL string) *HttpResponse {
r := NewHttp()
err := r.SetURL(URL)
if err != nil {
return nil
}
err = r.SetMethod("PUT")
if err != nil {
return nil
}
response, err := r.Do()
if err != nil {
log.Fatal(err)
}
return response
}
// DELETE sends a DELETE request to the specified URL and returns the HTTP response.
// If an error occurs during the request process, it will be logged and a nil response will be returned.
func DELETE(URL string) *HttpResponse {
r := NewHttp()
err := r.SetURL(URL)
if err != nil {
return nil
}
err = r.SetMethod("DELETE")
if err != nil {
return nil
}
response, err := r.Do()
if err != nil {
log.Fatal(err)
}
return response
}