Files
Hakushi_Crawl/utils/utils.go
2025-12-31 16:06:58 +07:00

35 lines
595 B
Go

package utils
import (
"encoding/json"
"fmt"
"net/http"
)
func GetĐataJSON[T any](url string) (*T, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
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, fmt.Errorf("http status %d", resp.StatusCode)
}
var data T
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}