Client
创建请求
Client
的以下这些方法会创建 HTTP 请求。
- R()
- NewRequest() - R 的别名.
- Get(url …string) - 创建 GET 请求,URL 是可选的。
- Post(url …string)
- Head(url …string)
- Delete(url …string)
- Put(url …string)
- Patch(url …string)
- Options(url …string)
创建并发下载
Client 设置
以下是 Client
设置相关的方法,它们都有对应的全局包装方法(测试时不需要显式创建 Client
,直接调用全局同名方法),基本上可以从方法命名就能直接看出设置的含义。
Debug 能力
- DevMode() - 启用所有 Debug 特性 (Dump, DebugLog 和 Trace)
- EnableDebugLog() - 启用 Debug 级别日志 (默认是关闭)
- DisableDebugLog()
- SetLogger(log Logger) - 自定义 Logger
- EnableTraceAll() - 为所有请求开启 Trace (默认是关闭)
- DisableTraceAll()
- EnableDumpAll() - dump 所有请求 (默认是关闭)
- DisableDumpAll()
- SetCommonDumpOptions(opt *DumpOptions)
- EnableDumpAllAsync()
- EnableDumpAllTo(output io.Writer)
- EnableDumpAllToFile(filename string)
- EnableDumpAllWithoutBody()
- EnableDumpAllWithoutHeader()
- EnableDumpAllWithoutRequest()
- EnableDumpAllWithoutRequestBody()
- EnableDumpAllWithoutResponse()
- EnableDumpAllWithoutResponseBody()
- EnableDumpEeachRequest() - 语法糖,利用 Request 中间件为每个请求单独开启 dump(不打印),在需要的时候获取 dump 内容 (默认是关闭)
- EnableDumpEeachRequestWithoutBody()
- EnableDumpEeachRequestWithoutHeader()
- EnableDumpEeachRequestWithoutResponse()
- EnableDumpEeachRequestWithoutRequest()
- EnableDumpEeachRequestWithoutRequestBody()
- EnableDumpEeachRequestWithoutResponseBody()
构造请求的共同设置
- SetCommonBasicAuth(username, password string)
- SetCommonBearerAuthToken(token string)
- SetCommonDigestAuth(username, password string)
- SetCommonContentType(ct string)
- SetCommonCookies(cookies …*http.Cookie)
- SetCommonFormData(data map[string]string)
- SetCommonFormDataFromValues(data url.Values)
- SetCommonHeader(key, value string)
- SetCommonHeaders(hdrs map[string]string)
- SetCommonHeaderOrder(keys …string)
- SetCommonPseudoHeaderOder(keys …string)
- SetCommonPathParam(key, value string)
- SetCommonPathParams(pathParams map[string]string)
- SetCommonQueryParam(key, value string)
- SetCommonQueryParams(params map[string]string)
- SetCommonQueryString(query string)
- AddCommonQueryParam(key, value string)
- SetUserAgent(userAgent string)
- SetCommonErrorResult(err interface{})
自动解码
- EnableAutoDecode() - 默认就是启用,自动探测字符集并解码到 utf-8
- DisableAutoDecode()
- SetAutoDecodeContentType(contentTypes …string)
- SetAutoDecodeAllContentType()
- SetAutoDecodeContentTypeFunc(fn func(contentType string) bool)
TLS 和证书
- SetCerts(certs …tls.Certificate)
- SetCertFromFile(certFile, keyFile string)
- SetRootCertsFromFile(pemFiles …string)
- SetRootCertFromString(pemContent string)
- EnableInsecureSkipVerify()
- DisableInsecureSkipVerify - 默认就是关闭
- SetTLSHandshakeTimeout(timeout time.Duration)
- SetTLSClientConfig(conf *tls.Config)
- SetTLSFingerprintChrome()
- SetTLSFingerprintFirefox()
- SetTLSFingerprintEdge()
- SetTLSFingerprintQQ()
- SetTLSFingerprintSafari()
- SetTLSFingerprint360()
- SetTLSFingerprintIOS()
- SetTLSFingerprintAndroid()
- SetTLSFingerprintRandomized()
Marshal 和 Unmarshal
- SetJsonUnmarshal(fn func(data []byte, v interface{}) error)
- SetJsonMarshal(fn func(v interface{}) ([]byte, error))
- SetXmlUnmarshal(fn func(data []byte, v interface{}) error)
- SetXmlMarshal(fn func(v interface{}) ([]byte, error))
中间件
HTTP 版本
自动重试
- SetCommonRetryCount(count int)
- SetCommonRetryInterval(getRetryIntervalFunc GetRetryIntervalFunc)
- SetCommonRetryFixedInterval(interval time.Duration)
- SetCommonRetryBackoffInterval(min, max time.Duration)
- SetCommonRetryHook(hook RetryHookFunc)
- AddCommonRetryHook(hook RetryHookFunc)
- SetCommonRetryCondition(condition RetryConditionFunc)
- AddCommonRetryCondition(condition RetryConditionFunc)
中间件
- WrapRoundTripFunc(funcs …RoundTripWrapperFunc) - Client 中间件
- WrapRoundTrip(wrappers …RoundTripWrapper) - Client 中间件
- OnBeforeRequest(m RequestMiddleware) - Request 中间件
- OnAfterResponse(m ResponseMiddleware) - Response 中间件
网络、代理和 URL
- SetTimeout(d time.Duration)
- SetDialTLS(fn func(ctx context.Context, network, addr string) (net.Conn, error))
- SetDial(fn func(ctx context.Context, network, addr string) (net.Conn, error))
- SetProxyURL(proxyUrl string)
- SetProxy(proxy func(*http.Request) (*urlpkg.URL, error))
- SetUnixSocket(file string)
- SetScheme(scheme string)
- SetBaseURL(u string)
H2C
- EnableH2C() - 默认不开启
- DisableH2C()
Keepalive、 Cookie、 压缩和重定向策略
- EnableKeepAlives() - 默认就是启用
- DisableKeepAlives()
- SetCookieJar(jar http.CookieJar)
- ClearCookies()
- SetRedirectPolicy(policies …RedirectPolicy)
- EnableCompression() - 默认就是启用
- DisableCompression()
下载、自动读 Response 和允许 GET 请求带 Body
- SetOutputDirectory(dir string) - 下载目录
- EnableAutoReadResponse() - 默认就是启用
- DisableAutoReadResponse()
- EnableAllowGetMethodPayload() - 默认就是启用
- DisableAllowGetMethodPayload()