forked from SpaiR/StrongDMM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreq.go
34 lines (27 loc) · 789 Bytes
/
req.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
package req
import (
"fmt"
"io"
"net/http"
)
func Get(url string) (body []byte, err error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("fail to create a request to [%s]: %w", url, err)
}
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("fail to get a response from [%s]: %w", url, err)
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return nil, fmt.Errorf("fail to get successful response: code [%d]", resp.StatusCode)
}
if body, err = io.ReadAll(resp.Body); err != nil {
return nil, fmt.Errorf("fail to read remote data: %w", err)
}
if err := resp.Body.Close(); err != nil {
return nil, fmt.Errorf("fail to close response: %w", err)
}
return body, nil
}