-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.go
More file actions
40 lines (32 loc) · 909 Bytes
/
request.go
File metadata and controls
40 lines (32 loc) · 909 Bytes
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
package jx3api
import (
"context"
"io"
"log/slog"
"net/http"
)
func (c *Client) request(ctx context.Context, endpoint string, body io.Reader) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, "POST", BASE_URL+endpoint, body)
if err != nil {
slog.Error("request: requester build error: " + err.Error())
return nil, err
}
req.Header.Add("content-type", "application/json")
if c.Opts.Token != "" {
slog.Debug("request: using token: " + c.Opts.Token)
req.Header.Add("token", c.Opts.Token)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
slog.Error("request: request error: " + err.Error())
return nil, err
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
slog.Error("request: reading response body error: " + err.Error())
return nil, err
}
return respBody, nil
}