This commit is contained in:
2026-08-25 22:35:44 +08:00
parent ec58641d09
commit f14423418a
23 changed files with 738 additions and 680 deletions

View File

@@ -1,17 +1,15 @@
package libs
import (
"fmt"
"io"
"net/http"
"strings"
"time"
)
func getJSON(rawURL string, params url.Values, timeout time.Duration) (map[string]any, error) {
if timeout <= 0 {
timeout = 5 * time.Second
}
if params != nil {
if strings.Contains(rawURL, "?") {
rawURL += "&" + params.Encode()
} else {
rawURL += "?" + params.Encode()
}
}
// GetJSON 请求 JSON 接口并返回对象。
func GetJSON(rawURL string, timeout time.Duration) ([]byte, error) {
req, err := http.NewRequest(http.MethodGet, rawURL, nil)
if err != nil {
return nil, err
@@ -31,9 +29,5 @@ func getJSON(rawURL string, params url.Values, timeout time.Duration) (map[strin
if resp.StatusCode >= 400 {
return nil, fmt.Errorf("http %d: %s", resp.StatusCode, strings.TrimSpace(string(body)))
}
out := map[string]any{}
if err := json.Unmarshal(body, &out); err != nil {
return nil, err
}
return out, nil
return body, nil
}