-
Notifications
You must be signed in to change notification settings - Fork 1
/
http.go
53 lines (43 loc) · 904 Bytes
/
http.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
package goc
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
)
func GetRequest(ctx context.Context, isPOST bool, url string, body []byte) (*http.Request, error) {
method := "GET"
if isPOST {
method = "POST"
}
req, err := http.NewRequestWithContext(ctx, method, url, nil)
if len(body) > 0 {
req.Body = io.NopCloser(bytes.NewBuffer(body))
}
req.Header.Set("Content-Type", "application/json")
return req, err
}
func SendRequest(req *http.Request, dataPointer any) error {
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if !strings.Contains(res.Status, "OK") {
fmt.Println("Unauthorized access, update Jira credentials.")
os.Exit(0)
}
body, err := io.ReadAll(res.Body)
if err != nil {
return err
}
err = json.Unmarshal(body, dataPointer)
if err != nil {
return err
}
return nil
}