|
@@ -11,6 +11,7 @@ import (
|
|
|
"net"
|
|
|
"net/http"
|
|
|
"net/url"
|
|
|
+ "path"
|
|
|
"strings"
|
|
|
"time"
|
|
|
)
|
|
@@ -63,7 +64,7 @@ func Get(ctx context.Context, urlString string, cbs ...Option) (res *http.Respon
|
|
|
req.Header.Set(k, v)
|
|
|
}
|
|
|
}
|
|
|
- return Do(ctx, req)
|
|
|
+ return do(ctx, req, opts)
|
|
|
}
|
|
|
|
|
|
func Post(ctx context.Context, urlString string, cbs ...Option) (res *http.Response, err error) {
|
|
@@ -113,12 +114,10 @@ func Post(ctx context.Context, urlString string, cbs ...Option) (res *http.Respo
|
|
|
req.Header.Set(k, v)
|
|
|
}
|
|
|
}
|
|
|
- req.Header.Set("Content-Type", contentType)
|
|
|
- return Do(ctx, req)
|
|
|
-}
|
|
|
-
|
|
|
-func Do(ctx context.Context, req *http.Request) (res *http.Response, err error) {
|
|
|
- return httpClient.Do(req.WithContext(ctx))
|
|
|
+ if contentType != "" {
|
|
|
+ req.Header.Set("Content-Type", contentType)
|
|
|
+ }
|
|
|
+ return do(ctx, req, opts)
|
|
|
}
|
|
|
|
|
|
func Request(ctx context.Context, urlString string, response any, cbs ...Option) (err error) {
|
|
@@ -151,7 +150,7 @@ func Request(ctx context.Context, urlString string, response any, cbs ...Option)
|
|
|
req.Header.Set(k, v)
|
|
|
}
|
|
|
}
|
|
|
- if res, err = Do(ctx, req); err != nil {
|
|
|
+ if res, err = do(ctx, req, opts); err != nil {
|
|
|
return
|
|
|
}
|
|
|
defer func() {
|
|
@@ -166,12 +165,28 @@ func Request(ctx context.Context, urlString string, response any, cbs ...Option)
|
|
|
return
|
|
|
}
|
|
|
contentType = strings.ToLower(res.Header.Get("Content-Type"))
|
|
|
- if strings.Contains(contentType, JSON) {
|
|
|
+ extName := path.Ext(req.URL.String())
|
|
|
+ if strings.Contains(contentType, JSON) || extName == ".json" {
|
|
|
err = json.NewDecoder(res.Body).Decode(response)
|
|
|
- } else if strings.Contains(contentType, XML) {
|
|
|
+ } else if strings.Contains(contentType, XML) || extName == ".xml" {
|
|
|
err = xml.NewDecoder(res.Body).Decode(response)
|
|
|
} else {
|
|
|
err = fmt.Errorf("unsupported content type: %s", contentType)
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+func do(ctx context.Context, req *http.Request, opts *Options) (res *http.Response, err error) {
|
|
|
+ if opts.Human {
|
|
|
+ if req.Header.Get("User-Agent") == "" {
|
|
|
+ req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.54")
|
|
|
+ }
|
|
|
+ if req.Header.Get("Referer") == "" {
|
|
|
+ req.Header.Set("Referer", req.URL.String())
|
|
|
+ }
|
|
|
+ if req.Header.Get("Accept") == "" {
|
|
|
+ req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return httpClient.Do(req.WithContext(ctx))
|
|
|
+}
|