Files
NBA_Go/utils/http_client.go
T
2025-05-08 00:32:49 -05:00

34 lines
604 B
Go

package utils
import (
"context"
"errors"
"io"
"net/http"
"time"
)
func GetJSON(url string) ([]byte, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errors.New("received non-200 response: " + resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}