-
Notifications
You must be signed in to change notification settings - Fork 5
/
goquests.go
86 lines (74 loc) · 1.86 KB
/
goquests.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
package goquests
import (
"bytes"
"encoding/json"
"net/http"
)
type Request struct {
URL string
Data map[string]interface{}
Headers map[string]string
}
type Response struct {
Status string
Headers interface{}
Body map[string]interface{}
}
func NewRequest(method string, r Request) (Response, error) {
postBody, _ := json.Marshal(r.Data)
Body := bytes.NewBuffer(postBody)
req, err := http.NewRequest(method, r.URL, Body)
if err != nil {
return Response{}, err
}
for key, value := range r.Headers {
req.Header.Set(key, value)
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
rawresp, err := client.Do(req)
if err != nil {
return Response{}, err
}
defer rawresp.Body.Close()
response := Response{}
response.Status = rawresp.Status
response.Headers = rawresp.Header
var body map[string]interface{}
json.NewDecoder(rawresp.Body).Decode(&body)
response.Body = body
return response, nil
}
func Post(option Request) (Response, error) {
response, err := NewRequest("POST", option)
return response, err
}
/***/
func Get(option Request) (Response, error) {
response, err := NewRequest("GET", option)
return response, err
}
func Put(option Request) (Response, error) {
response, err := NewRequest("PUT", option)
return response, err
}
func Patch(option Request) (Response, error) {
response, err := NewRequest("PATCH", option)
return response, err
}
func Delete(option Request) (Response, error) {
response, err := NewRequest("DELETE", option)
return response, err
}
func Trace(option Request) (Response, error) {
response, err := NewRequest("TRACE", option)
return response, err
}
func Options(option Request) (Response, error) {
response, err := NewRequest("OPTIONS", option)
return response, err
}
func Head(option Request) (Response, error) {
response, err := NewRequest("Head", option)
return response, err
}