49 lines
1.0 KiB
Go
49 lines
1.0 KiB
Go
package egressutil
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func HTTPGetBody(client *http.Client, rawURL string, timeout time.Duration, userAgent string, maxBytes int64) (string, error) {
|
|
if client == nil {
|
|
return "", fmt.Errorf("http client is nil")
|
|
}
|
|
limit := maxBytes
|
|
if limit <= 0 {
|
|
limit = 8 * 1024
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
|
defer cancel()
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, nil)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if strings.TrimSpace(userAgent) != "" {
|
|
req.Header.Set("User-Agent", strings.TrimSpace(userAgent))
|
|
}
|
|
req.Header.Set("Accept", "application/json, text/plain, */*")
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, _ := io.ReadAll(io.LimitReader(resp.Body, limit))
|
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
msg := strings.TrimSpace(string(body))
|
|
if msg == "" {
|
|
msg = resp.Status
|
|
}
|
|
return "", fmt.Errorf("%s -> %s", rawURL, msg)
|
|
}
|
|
return string(body), nil
|
|
}
|