-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
http.go
30 lines (25 loc) · 657 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
package main
import (
"fmt"
"io"
"net/http"
)
func (a *app) httpRequest(url string) ([]byte, error) {
req, err := http.NewRequestWithContext(a.ctx, http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("could create get request for %s: %w", url, err)
}
resp, err := a.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("could not get %s: %w", url, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %s", url, resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("could not read body from %s: %w", url, err)
}
return body, nil
}