Documentation
¶
Index ¶
- func Delete[T any](url string, opts ...RequestOption) (*T, error)
- func DeleteAny(url string, opts ...RequestOption) (any, error)
- func DeleteAnyC(client *Client, url string, opts ...RequestOption) (any, error)
- func DeleteBody[T any](url string, body interface{}, opts ...RequestOption) (*T, error)
- func DeleteBodyAny(url string, body interface{}, opts ...RequestOption) (any, error)
- func DeleteBodyAnyC(client *Client, url string, body interface{}, opts ...RequestOption) (any, error)
- func DeleteBodyC[T any](client *Client, url string, body interface{}, opts ...RequestOption) (*T, error)
- func DeleteBodyRetry[T any](url string, body interface{}, retries int, opts ...RequestOption) (*T, error)
- func DeleteBodyRetryC[T any](client *Client, url string, body interface{}, retries int, ...) (*T, error)
- func DeleteC[T any](client *Client, url string, opts ...RequestOption) (*T, error)
- func DeleteRetry[T any](url string, retries int, opts ...RequestOption) (*T, error)
- func DeleteRetryC[T any](client *Client, url string, retries int, opts ...RequestOption) (*T, error)
- func Do[T any](method, url string, opts ...RequestOption) (*T, error)
- func DoAny(method, url string, opts ...RequestOption) (any, error)
- func DoAnyC(client *Client, method, url string, opts ...RequestOption) (any, error)
- func DoC[T any](client *Client, method, url string, opts ...RequestOption) (*T, error)
- func DoRetry[T any](method, url string, retries int, opts ...RequestOption) (*T, error)
- func DoRetryC[T any](client *Client, method, url string, retries int, opts ...RequestOption) (*T, error)
- func Get[T any](url string, opts ...RequestOption) (*T, error)
- func GetAny(url string, opts ...RequestOption) (any, error)
- func GetAnyC(client *Client, url string, opts ...RequestOption) (any, error)
- func GetC[T any](client *Client, url string, opts ...RequestOption) (*T, error)
- func GetRetry[T any](url string, retries int, opts ...RequestOption) (*T, error)
- func GetRetryC[T any](client *Client, url string, retries int, opts ...RequestOption) (*T, error)
- func Patch[T any](url string, opts ...RequestOption) (*T, error)
- func PatchAny(url string, opts ...RequestOption) (any, error)
- func PatchAnyC(client *Client, url string, opts ...RequestOption) (any, error)
- func PatchBody[T any](url string, body interface{}, opts ...RequestOption) (*T, error)
- func PatchBodyAny(url string, body interface{}, opts ...RequestOption) (any, error)
- func PatchBodyAnyC(client *Client, url string, body interface{}, opts ...RequestOption) (any, error)
- func PatchBodyC[T any](client *Client, url string, body interface{}, opts ...RequestOption) (*T, error)
- func PatchBodyRetry[T any](url string, body interface{}, retries int, opts ...RequestOption) (*T, error)
- func PatchBodyRetryC[T any](client *Client, url string, body interface{}, retries int, ...) (*T, error)
- func PatchC[T any](client *Client, url string, opts ...RequestOption) (*T, error)
- func PatchRetry[T any](url string, retries int, opts ...RequestOption) (*T, error)
- func PatchRetryC[T any](client *Client, url string, retries int, opts ...RequestOption) (*T, error)
- func Post[T any](url string, opts ...RequestOption) (*T, error)
- func PostAny(url string, opts ...RequestOption) (any, error)
- func PostAnyC(client *Client, url string, opts ...RequestOption) (any, error)
- func PostBody[T any](url string, body interface{}, opts ...RequestOption) (*T, error)
- func PostBodyAny(url string, body interface{}, opts ...RequestOption) (any, error)
- func PostBodyAnyC(client *Client, url string, body interface{}, opts ...RequestOption) (any, error)
- func PostBodyC[T any](client *Client, url string, body interface{}, opts ...RequestOption) (*T, error)
- func PostBodyRetry[T any](url string, body interface{}, retries int, opts ...RequestOption) (*T, error)
- func PostBodyRetryC[T any](client *Client, url string, body interface{}, retries int, ...) (*T, error)
- func PostC[T any](client *Client, url string, opts ...RequestOption) (*T, error)
- func PostRetry[T any](url string, retries int, opts ...RequestOption) (*T, error)
- func PostRetryC[T any](client *Client, url string, retries int, opts ...RequestOption) (*T, error)
- func Put[T any](url string, opts ...RequestOption) (*T, error)
- func PutAny(url string, opts ...RequestOption) (any, error)
- func PutAnyC(client *Client, url string, opts ...RequestOption) (any, error)
- func PutBody[T any](url string, body interface{}, opts ...RequestOption) (*T, error)
- func PutBodyAny(url string, body interface{}, opts ...RequestOption) (any, error)
- func PutBodyAnyC(client *Client, url string, body interface{}, opts ...RequestOption) (any, error)
- func PutBodyC[T any](client *Client, url string, body interface{}, opts ...RequestOption) (*T, error)
- func PutBodyRetry[T any](url string, body interface{}, retries int, opts ...RequestOption) (*T, error)
- func PutBodyRetryC[T any](client *Client, url string, body interface{}, retries int, ...) (*T, error)
- func PutC[T any](client *Client, url string, opts ...RequestOption) (*T, error)
- func PutRetry[T any](url string, retries int, opts ...RequestOption) (*T, error)
- func PutRetryC[T any](client *Client, url string, retries int, opts ...RequestOption) (*T, error)
- func SetDefaultClient(client *Client)
- func SetDefaultInsecure(insecure bool)
- type Client
- func (c *Client) Delete(url string, opts ...RequestOption) *Response
- func (c *Client) Do(method, url string, opts ...RequestOption) *Response
- func (c *Client) Get(url string, opts ...RequestOption) *Response
- func (c *Client) Head(url string, opts ...RequestOption) *Response
- func (c *Client) Options(url string, opts ...RequestOption) *Response
- func (c *Client) Patch(url string, opts ...RequestOption) *Response
- func (c *Client) Post(url string, opts ...RequestOption) *Response
- func (c *Client) Put(url string, opts ...RequestOption) *Response
- func (c *Client) SetInsecure(insecure bool)
- func (c *Client) SetResponseInterceptor(interceptor ResponseInterceptor)
- type ClientOption
- func WithAuthToken(token string) ClientOption
- func WithBaseURL(baseURL string) ClientOption
- func WithBasicAuth(username, password string) ClientOption
- func WithClientTimeout(timeout time.Duration) ClientOption
- func WithDebug(debug bool) ClientOption
- func WithHeaders(headers map[string]string) ClientOption
- func WithInsecure(insecure bool) ClientOption
- func WithResponseInterceptor(interceptor ResponseInterceptor) ClientOption
- func WithRetries(retries int) ClientOption
- func WithUserAgent(userAgent string) ClientOption
- type ErrorType
- type HttpError
- type RequestOption
- func WithBody(body interface{}) RequestOption
- func WithFormData(data map[string]string) RequestOption
- func WithHeader(key, value string) RequestOption
- func WithPathParam(key, value string) RequestOption
- func WithPathParams(params map[string]string) RequestOption
- func WithQueryParam(key, value string) RequestOption
- func WithQueryParams(params map[string]string) RequestOption
- func WithRequestHeaders(headers map[string]string) RequestOption
- func WithRequestTimeout(timeout time.Duration) RequestOption
- func WithRetry(retries int) RequestOption
- func WithUnmarshalOption(opts ...cystructmap.Option) RequestOption
- type Response
- type ResponseInterceptor
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Delete ¶
func Delete[T any](url string, opts ...RequestOption) (*T, error)
Delete 发送 DELETE 请求并返回泛型响应。
参数:
- url: 请求的 URL 地址
- opts: 可选的请求配置选项
返回值:
- *T: 反序列化后的响应数据指针
- error: 如果请求失败或响应解析失败返回错误
用法:
resp, err := http.Delete[Response]("https://api.example.com/users/1")
特点:
- 使用全局默认客户端
- 自动反序列化 JSON 响应
- 支持自定义请求选项
func DeleteAny ¶
func DeleteAny(url string, opts ...RequestOption) (any, error)
DeleteAny 发送 DELETE 请求并返回 any 类型结果。
func DeleteAnyC ¶
func DeleteAnyC(client *Client, url string, opts ...RequestOption) (any, error)
DeleteAnyC 发送 DELETE 请求并返回 any 类型结果
func DeleteBody ¶
func DeleteBody[T any](url string, body interface{}, opts ...RequestOption) (*T, error)
DeleteBody 发送 DELETE 请求(带 body)并返回泛型响应。
参数:
- url: 请求的 URL 地址
- body: 请求体数据(会自动序列化为 JSON)
- opts: 可选的请求配置选项
返回值:
- *T: 反序列化后的响应数据指针
- error: 如果请求失败或响应解析失败返回错误
用法:
resp, err := http.DeleteBody[Response]("https://api.example.com/users/1",
map[string]string{"reason": "user_request"},
)
特点:
- 简化了 body 传递,无需使用 WithBody 选项
- 自动序列化 body 为 JSON
- 使用全局默认客户端
func DeleteBodyAny ¶
func DeleteBodyAny(url string, body interface{}, opts ...RequestOption) (any, error)
DeleteBodyAny 发送 DELETE 请求(带 body)并返回 any 类型结果。
func DeleteBodyAnyC ¶
func DeleteBodyAnyC(client *Client, url string, body interface{}, opts ...RequestOption) (any, error)
DeleteBodyAnyC 发送 DELETE 请求(带 body)并返回 any 类型结果
func DeleteBodyC ¶
func DeleteBodyC[T any](client *Client, url string, body interface{}, opts ...RequestOption) (*T, error)
DeleteBodyC 发送 DELETE 请求(带 body)并返回泛型响应
func DeleteBodyRetry ¶
func DeleteBodyRetry[T any](url string, body interface{}, retries int, opts ...RequestOption) (*T, error)
DeleteBodyRetry 发送 DELETE 请求(带 body),支持自定义重试策略。
参数:
- url: 请求的 URL 地址
- body: 请求体数据(会自动序列化为 JSON)
- retries: 重试次数
- opts: 可选的请求配置选项
返回值:
- *T: 反序列化后的响应数据指针
- error: 如果请求失败或响应解析失败返回错误
用法:
resp, err := http.DeleteBodyRetry[Response]("https://api.example.com/users/1",
map[string]string{"reason": "user_request"},
3,
)
特点:
- 结合了 body 便利和重试功能
- 简化了 body 传递,无需使用 WithBody 选项
- 支持自定义重试次数
- 使用全局默认客户端
func DeleteBodyRetryC ¶
func DeleteBodyRetryC[T any](client *Client, url string, body interface{}, retries int, opts ...RequestOption) (*T, error)
DeleteBodyRetryC 发送 DELETE 请求(带 body),支持自定义重试策略
func DeleteC ¶
func DeleteC[T any](client *Client, url string, opts ...RequestOption) (*T, error)
DeleteC 发送 DELETE 请求并返回泛型响应
func DeleteRetry ¶
func DeleteRetry[T any](url string, retries int, opts ...RequestOption) (*T, error)
DeleteRetry 发送 DELETE 请求,支持自定义重试策略。
参数:
- url: 请求的 URL 地址
- retries: 重试次数
- opts: 可选的请求配置选项
返回值:
- *T: 反序列化后的响应数据指针
- error: 如果请求失败或响应解析失败返回错误
用法:
resp, err := http.DeleteRetry[Response]("https://api.example.com/users/1", 3)
特点:
- 支持自定义重试次数
- 使用全局默认客户端
- 自动反序列化 JSON 响应
func DeleteRetryC ¶
func DeleteRetryC[T any](client *Client, url string, retries int, opts ...RequestOption) (*T, error)
DeleteRetryC 发送 DELETE 请求,支持自定义重试策略
func Do ¶
func Do[T any](method, url string, opts ...RequestOption) (*T, error)
Do 发送指定 HTTP 方法的请求并返回泛型响应。
func DoAny ¶
func DoAny(method, url string, opts ...RequestOption) (any, error)
DoAny 发送指定 HTTP 方法的请求并返回 any 类型结果。
func DoAnyC ¶
func DoAnyC(client *Client, method, url string, opts ...RequestOption) (any, error)
DoAnyC 发送指定 HTTP 方法的请求并返回 any 类型结果
func DoC ¶
func DoC[T any](client *Client, method, url string, opts ...RequestOption) (*T, error)
DoC 发送指定 HTTP 方法的请求并返回泛型响应
func DoRetry ¶
func DoRetry[T any](method, url string, retries int, opts ...RequestOption) (*T, error)
DoRetry 发送指定 HTTP 方法的请求,支持自定义重试策略。
func DoRetryC ¶
func DoRetryC[T any](client *Client, method, url string, retries int, opts ...RequestOption) (*T, error)
DoRetryC 发送指定 HTTP 方法的请求,支持自定义重试策略
func Get ¶
func Get[T any](url string, opts ...RequestOption) (*T, error)
Get 发送 GET 请求并返回泛型响应。
参数:
- url: 请求的 URL 地址
- opts: 可选的请求配置选项
返回值:
- *T: 反序列化后的响应数据指针
- error: 如果请求失败或响应解析失败返回错误
用法:
type User struct {
ID int `json:"id"`
Name string `json:"name"`
}
user, err := http.Get[User]("https://api.example.com/users/1")
特点:
- 使用全局默认客户端
- 自动反序列化 JSON 响应
- 支持自定义请求选项
局限:
- 响应必须是有效的 JSON 格式
- 必须提供正确的类型参数
func GetAny ¶
func GetAny(url string, opts ...RequestOption) (any, error)
GetAny 发送 GET 请求并返回 any 类型结果。
func GetAnyC ¶
func GetAnyC(client *Client, url string, opts ...RequestOption) (any, error)
GetAnyC 发送 GET 请求并返回 any 类型结果
func GetC ¶
func GetC[T any](client *Client, url string, opts ...RequestOption) (*T, error)
GetC 发送 GET 请求并返回泛型响应
func GetRetry ¶
func GetRetry[T any](url string, retries int, opts ...RequestOption) (*T, error)
GetRetry 发送 GET 请求,支持自定义重试策略。
参数:
- url: 请求的 URL 地址
- retries: 重试次数
- opts: 可选的请求配置选项
返回值:
- *T: 反序列化后的响应数据指针
- error: 如果请求失败或响应解析失败返回错误
用法:
user, err := http.GetRetry[User]("https://api.example.com/users/1", 3)
特点:
- 支持自定义重试次数
- 使用全局默认客户端
- 自动反序列化 JSON 响应
局限:
- 重试次数过多可能导致请求延迟增加
func Patch ¶
func Patch[T any](url string, opts ...RequestOption) (*T, error)
Patch 发送 PATCH 请求并返回泛型响应。
参数:
- url: 请求的 URL 地址
- opts: 可选的请求配置选项(包括请求体)
返回值:
- *T: 反序列化后的响应数据指针
- error: 如果请求失败或响应解析失败返回错误
用法:
resp, err := http.Patch[Response]("https://api.example.com/users/1",
http.WithBody(map[string]string{"status": "active"}),
)
特点:
- 使用全局默认客户端
- 支持通过 WithBody 选项传递请求体
- 自动反序列化 JSON 响应
func PatchAny ¶
func PatchAny(url string, opts ...RequestOption) (any, error)
PatchAny 发送 PATCH 请求并返回 any 类型结果。
func PatchAnyC ¶
func PatchAnyC(client *Client, url string, opts ...RequestOption) (any, error)
PatchAnyC 发送 PATCH 请求并返回 any 类型结果
func PatchBody ¶
func PatchBody[T any](url string, body interface{}, opts ...RequestOption) (*T, error)
PatchBody 发送 PATCH 请求(带 body)并返回泛型响应。
参数:
- url: 请求的 URL 地址
- body: 请求体数据(会自动序列化为 JSON)
- opts: 可选的请求配置选项
返回值:
- *T: 反序列化后的响应数据指针
- error: 如果请求失败或响应解析失败返回错误
用法:
resp, err := http.PatchBody[Response]("https://api.example.com/users/1",
map[string]string{"status": "active"},
)
特点:
- 简化了 body 传递,无需使用 WithBody 选项
- 自动序列化 body 为 JSON
- 使用全局默认客户端
func PatchBodyAny ¶
func PatchBodyAny(url string, body interface{}, opts ...RequestOption) (any, error)
PatchBodyAny 发送 PATCH 请求(带 body)并返回 any 类型结果。
func PatchBodyAnyC ¶
func PatchBodyAnyC(client *Client, url string, body interface{}, opts ...RequestOption) (any, error)
PatchBodyAnyC 发送 PATCH 请求(带 body)并返回 any 类型结果
func PatchBodyC ¶
func PatchBodyC[T any](client *Client, url string, body interface{}, opts ...RequestOption) (*T, error)
PatchBodyC 发送 PATCH 请求(带 body)并返回泛型响应
func PatchBodyRetry ¶
func PatchBodyRetry[T any](url string, body interface{}, retries int, opts ...RequestOption) (*T, error)
PatchBodyRetry 发送 PATCH 请求(带 body),支持自定义重试策略。
参数:
- url: 请求的 URL 地址
- body: 请求体数据(会自动序列化为 JSON)
- retries: 重试次数
- opts: 可选的请求配置选项
返回值:
- *T: 反序列化后的响应数据指针
- error: 如果请求失败或响应解析失败返回错误
用法:
resp, err := http.PatchBodyRetry[Response]("https://api.example.com/users/1",
map[string]string{"status": "active"},
3,
)
特点:
- 结合了 body 便利和重试功能
- 简化了 body 传递,无需使用 WithBody 选项
- 支持自定义重试次数
- 使用全局默认客户端
func PatchBodyRetryC ¶
func PatchBodyRetryC[T any](client *Client, url string, body interface{}, retries int, opts ...RequestOption) (*T, error)
PatchBodyRetryC 发送 PATCH 请求(带 body),支持自定义重试策略
func PatchC ¶
func PatchC[T any](client *Client, url string, opts ...RequestOption) (*T, error)
PatchC 发送 PATCH 请求并返回泛型响应
func PatchRetry ¶
func PatchRetry[T any](url string, retries int, opts ...RequestOption) (*T, error)
PatchRetry 发送 PATCH 请求,支持自定义重试策略。
参数:
- url: 请求的 URL 地址
- retries: 重试次数
- opts: 可选的请求配置选项(包括请求体)
返回值:
- *T: 反序列化后的响应数据指针
- error: 如果请求失败或响应解析失败返回错误
用法:
resp, err := http.PatchRetry[Response]("https://api.example.com/users/1", 3,
http.WithBody(map[string]string{"status": "active"}),
)
特点:
- 支持自定义重试次数
- 使用全局默认客户端
- 自动反序列化 JSON 响应
func PatchRetryC ¶
PatchRetryC 发送 PATCH 请求,支持自定义重试策略
func Post ¶
func Post[T any](url string, opts ...RequestOption) (*T, error)
Post 发送 POST 请求并返回泛型响应。
参数:
- url: 请求的 URL 地址
- opts: 可选的请求配置选项(包括请求体)
返回值:
- *T: 反序列化后的响应数据指针
- error: 如果请求失败或响应解析失败返回错误
用法:
type Response struct {
Code int `json:"code"`
Data string `json:"data"`
}
resp, err := http.Post[Response]("https://api.example.com/users",
http.WithBody(map[string]string{"name": "John"}),
)
特点:
- 使用全局默认客户端
- 支持通过 WithBody 选项传递请求体
- 自动反序列化 JSON 响应
局限:
- 响应必须是有效的 JSON 格式
- 必须通过 WithBody 选项传递请求体
func PostAny ¶
func PostAny(url string, opts ...RequestOption) (any, error)
PostAny 发送 POST 请求并返回 any 类型结果。
func PostAnyC ¶
func PostAnyC(client *Client, url string, opts ...RequestOption) (any, error)
PostAnyC 发送 POST 请求并返回 any 类型结果
func PostBody ¶
func PostBody[T any](url string, body interface{}, opts ...RequestOption) (*T, error)
PostBody 发送 POST 请求(带 body)并返回泛型响应。
参数:
- url: 请求的 URL 地址
- body: 请求体数据(会自动序列化为 JSON)
- opts: 可选的请求配置选项
返回值:
- *T: 反序列化后的响应数据指针
- error: 如果请求失败或响应解析失败返回错误
用法:
resp, err := http.PostBody[Response]("https://api.example.com/users",
map[string]string{"name": "John", "email": "john@example.com"},
)
特点:
- 简化了 body 传递,无需使用 WithBody 选项
- 自动序列化 body 为 JSON
- 使用全局默认客户端
func PostBodyAny ¶
func PostBodyAny(url string, body interface{}, opts ...RequestOption) (any, error)
PostBodyAny 发送 POST 请求(带 body)并返回 any 类型结果。
func PostBodyAnyC ¶
func PostBodyAnyC(client *Client, url string, body interface{}, opts ...RequestOption) (any, error)
PostBodyAnyC 发送 POST 请求(带 body)并返回 any 类型结果
func PostBodyC ¶
func PostBodyC[T any](client *Client, url string, body interface{}, opts ...RequestOption) (*T, error)
PostBodyC 发送 POST 请求(带 body)并返回泛型响应
func PostBodyRetry ¶
func PostBodyRetry[T any](url string, body interface{}, retries int, opts ...RequestOption) (*T, error)
PostBodyRetry 发送 POST 请求(带 body),支持自定义重试策略。
参数:
- url: 请求的 URL 地址
- body: 请求体数据(会自动序列化为 JSON)
- retries: 重试次数
- opts: 可选的请求配置选项
返回值:
- *T: 反序列化后的响应数据指针
- error: 如果请求失败或响应解析失败返回错误
用法:
resp, err := http.PostBodyRetry[Response]("https://api.example.com/users",
map[string]string{"name": "John"},
3,
)
特点:
- 结合了 body 便利和重试功能
- 简化了 body 传递,无需使用 WithBody 选项
- 支持自定义重试次数
- 使用全局默认客户端
func PostBodyRetryC ¶
func PostBodyRetryC[T any](client *Client, url string, body interface{}, retries int, opts ...RequestOption) (*T, error)
PostBodyRetryC 发送 POST 请求(带 body),支持自定义重试策略
func PostC ¶
func PostC[T any](client *Client, url string, opts ...RequestOption) (*T, error)
PostC 发送 POST 请求并返回泛型响应
func PostRetry ¶
func PostRetry[T any](url string, retries int, opts ...RequestOption) (*T, error)
PostRetry 发送 POST 请求,支持自定义重试策略。
参数:
- url: 请求的 URL 地址
- retries: 重试次数
- opts: 可选的请求配置选项(包括请求体)
返回值:
- *T: 反序列化后的响应数据指针
- error: 如果请求失败或响应解析失败返回错误
用法:
resp, err := http.PostRetry[Response]("https://api.example.com/users", 3,
http.WithBody(map[string]string{"name": "John"}),
)
特点:
- 支持自定义重试次数
- 使用全局默认客户端
- 自动反序列化 JSON 响应
func PostRetryC ¶
PostRetryC 发送 POST 请求,支持自定义重试策略
func Put ¶
func Put[T any](url string, opts ...RequestOption) (*T, error)
Put 发送 PUT 请求并返回泛型响应。
参数:
- url: 请求的 URL 地址
- opts: 可选的请求配置选项(包括请求体)
返回值:
- *T: 反序列化后的响应数据指针
- error: 如果请求失败或响应解析失败返回错误
用法:
resp, err := http.Put[Response]("https://api.example.com/users/1",
http.WithBody(map[string]string{"name": "Jane"}),
)
特点:
- 使用全局默认客户端
- 支持通过 WithBody 选项传递请求体
- 自动反序列化 JSON 响应
func PutAny ¶
func PutAny(url string, opts ...RequestOption) (any, error)
PutAny 发送 PUT 请求并返回 any 类型结果。
func PutAnyC ¶
func PutAnyC(client *Client, url string, opts ...RequestOption) (any, error)
PutAnyC 发送 PUT 请求并返回 any 类型结果
func PutBody ¶
func PutBody[T any](url string, body interface{}, opts ...RequestOption) (*T, error)
PutBody 发送 PUT 请求(带 body)并返回泛型响应。
参数:
- url: 请求的 URL 地址
- body: 请求体数据(会自动序列化为 JSON)
- opts: 可选的请求配置选项
返回值:
- *T: 反序列化后的响应数据指针
- error: 如果请求失败或响应解析失败返回错误
用法:
resp, err := http.PutBody[Response]("https://api.example.com/users/1",
map[string]string{"name": "Jane"},
)
特点:
- 简化了 body 传递,无需使用 WithBody 选项
- 自动序列化 body 为 JSON
- 使用全局默认客户端
func PutBodyAny ¶
func PutBodyAny(url string, body interface{}, opts ...RequestOption) (any, error)
PutBodyAny 发送 PUT 请求(带 body)并返回 any 类型结果。
func PutBodyAnyC ¶
func PutBodyAnyC(client *Client, url string, body interface{}, opts ...RequestOption) (any, error)
PutBodyAnyC 发送 PUT 请求(带 body)并返回 any 类型结果
func PutBodyC ¶
func PutBodyC[T any](client *Client, url string, body interface{}, opts ...RequestOption) (*T, error)
PutBodyC 发送 PUT 请求(带 body)并返回泛型响应
func PutBodyRetry ¶
func PutBodyRetry[T any](url string, body interface{}, retries int, opts ...RequestOption) (*T, error)
PutBodyRetry 发送 PUT 请求(带 body),支持自定义重试策略。
参数:
- url: 请求的 URL 地址
- body: 请求体数据(会自动序列化为 JSON)
- retries: 重试次数
- opts: 可选的请求配置选项
返回值:
- *T: 反序列化后的响应数据指针
- error: 如果请求失败或响应解析失败返回错误
用法:
resp, err := http.PutBodyRetry[Response]("https://api.example.com/users/1",
map[string]string{"name": "Jane"},
3,
)
特点:
- 结合了 body 便利和重试功能
- 简化了 body 传递,无需使用 WithBody 选项
- 支持自定义重试次数
- 使用全局默认客户端
func PutBodyRetryC ¶
func PutBodyRetryC[T any](client *Client, url string, body interface{}, retries int, opts ...RequestOption) (*T, error)
PutBodyRetryC 发送 PUT 请求(带 body),支持自定义重试策略
func PutC ¶
func PutC[T any](client *Client, url string, opts ...RequestOption) (*T, error)
PutC 发送 PUT 请求并返回泛型响应
func PutRetry ¶
func PutRetry[T any](url string, retries int, opts ...RequestOption) (*T, error)
PutRetry 发送 PUT 请求,支持自定义重试策略。
参数:
- url: 请求的 URL 地址
- retries: 重试次数
- opts: 可选的请求配置选项(包括请求体)
返回值:
- *T: 反序列化后的响应数据指针
- error: 如果请求失败或响应解析失败返回错误
用法:
resp, err := http.PutRetry[Response]("https://api.example.com/users/1", 3,
http.WithBody(map[string]string{"name": "Jane"}),
)
特点:
- 支持自定义重试次数
- 使用全局默认客户端
- 自动反序列化 JSON 响应
func SetDefaultInsecure ¶
func SetDefaultInsecure(insecure bool)
SetDefaultInsecure 设置全局默认客户端的不安全访问选项 注意:仅在开发或测试环境中使用,生产环境不推荐
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client 是 HTTP 客户端的包装器
func (*Client) Delete ¶
func (c *Client) Delete(url string, opts ...RequestOption) *Response
Delete 发送 DELETE 请求
func (*Client) Do ¶
func (c *Client) Do(method, url string, opts ...RequestOption) *Response
Do 执行指定的 HTTP 方法请求
func (*Client) Get ¶
func (c *Client) Get(url string, opts ...RequestOption) *Response
Get 发送 GET 请求
func (*Client) Head ¶
func (c *Client) Head(url string, opts ...RequestOption) *Response
Head 发送 HEAD 请求
func (*Client) Options ¶
func (c *Client) Options(url string, opts ...RequestOption) *Response
Options 发送 OPTIONS 请求
func (*Client) Patch ¶
func (c *Client) Patch(url string, opts ...RequestOption) *Response
Patch 发送 PATCH 请求
func (*Client) Post ¶
func (c *Client) Post(url string, opts ...RequestOption) *Response
Post 发送 POST 请求
func (*Client) Put ¶
func (c *Client) Put(url string, opts ...RequestOption) *Response
Put 发送 PUT 请求
func (*Client) SetResponseInterceptor ¶
func (c *Client) SetResponseInterceptor(interceptor ResponseInterceptor)
SetResponseInterceptor 动态设置响应拦截器
type ClientOption ¶
type ClientOption func(*Client)
ClientOption 是 HTTP 客户端的配置选项
func WithAuthToken ¶
func WithAuthToken(token string) ClientOption
WithAuthToken 设置认证令牌(Bearer token)
func WithBasicAuth ¶
func WithBasicAuth(username, password string) ClientOption
WithBasicAuth 设置基本认证
func WithClientTimeout ¶
func WithClientTimeout(timeout time.Duration) ClientOption
WithClientTimeout 设置请求超时时间
func WithInsecure ¶
func WithInsecure(insecure bool) ClientOption
WithInsecure 允许不安全的 HTTPS 访问(跳过 SSL/TLS 证书验证) 注意:仅在开发或测试环境中使用,生产环境不推荐
func WithResponseInterceptor ¶
func WithResponseInterceptor(interceptor ResponseInterceptor) ClientOption
WithResponseInterceptor 设置响应拦截器,在每次 HTTP 响应返回前调用
type HttpError ¶
type HttpError struct {
// Type 错误类型
Type ErrorType
// StatusCode HTTP 状态码(仅当 Type 为 ErrorTypeHTTP 时有效)
StatusCode int
// Message 错误信息
Message string
// Err 原始错误
Err error
}
HttpError 是 HTTP 请求的错误类型。
用于区分不同类型的错误:
- 网络问题:连接失败、超时、DNS 解析失败等
- HTTP 错误:服务器返回非 2xx 状态码
- 解析错误:响应体解析失败
用法:
user, err := http.Get[User]("/users/1")
if err != nil {
if httpErr, ok := err.(*HttpError); ok {
switch httpErr.Type {
case http.ErrorTypeNetwork:
// 处理网络问题
fmt.Println("网络错误:", httpErr.Message)
case http.ErrorTypeHTTP:
// 处理 HTTP 错误
fmt.Printf("HTTP %d: %s\n", httpErr.StatusCode, httpErr.Message)
case http.ErrorTypeParse:
// 处理解析错误
fmt.Println("解析错误:", httpErr.Message)
}
}
}
func (*HttpError) IsNetworkError ¶
IsNetworkError 检查是否是网络错误
type RequestOption ¶
RequestOption 是单个请求的配置选项
func WithPathParams ¶
func WithPathParams(params map[string]string) RequestOption
WithPathParams 设置多个路径参数
func WithQueryParams ¶
func WithQueryParams(params map[string]string) RequestOption
WithQueryParams 添加多个查询参数
func WithRequestHeaders ¶
func WithRequestHeaders(headers map[string]string) RequestOption
WithRequestHeaders 为单个请求添加多个请求头
func WithRequestTimeout ¶
func WithRequestTimeout(timeout time.Duration) RequestOption
WithRequestTimeout 为单个请求设置超时时间
func WithRetry ¶
func WithRetry(retries int) RequestOption
WithRetry 为单个请求设置重试次数(注:resty 在 Request 级别不支持,此为占位符)
func WithUnmarshalOption ¶
func WithUnmarshalOption(opts ...cystructmap.Option) RequestOption
WithUnmarshalOption 为单个请求设置反序列化配置 用于在请求级别自定义反序列化行为(如类型转换选项等)
type Response ¶
type Response struct {
StatusCode int
Body []byte
Headers map[string][]string
Error error
UnmarshalOpts []cystructmap.Option // 反序列化配置
}
Response 是响应的包装器
type ResponseInterceptor ¶
ResponseInterceptor 响应拦截器,在 HTTP 方法返回 Response 之前调用。 可以修改或替换 Response(如注入错误消息)。