cyhttp

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

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 GetRetryC

func GetRetryC[T any](client *Client, url string, retries int, opts ...RequestOption) (*T, error)

GetRetryC 发送 GET 请求,支持自定义重试策略

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

func PatchRetryC[T any](client *Client, url string, retries int, opts ...RequestOption) (*T, error)

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

func PostRetryC[T any](client *Client, url string, retries int, opts ...RequestOption) (*T, error)

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 PutRetryC

func PutRetryC[T any](client *Client, url string, retries int, opts ...RequestOption) (*T, error)

PutRetryC 发送 PUT 请求,支持自定义重试策略

func SetDefaultClient

func SetDefaultClient(client *Client)

SetDefaultClient 设置全局默认客户端

func SetDefaultInsecure

func SetDefaultInsecure(insecure bool)

SetDefaultInsecure 设置全局默认客户端的不安全访问选项 注意:仅在开发或测试环境中使用,生产环境不推荐

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client 是 HTTP 客户端的包装器

func GetDefaultClient

func GetDefaultClient() *Client

GetDefaultClient 获取全局默认客户端

func NewClient

func NewClient(opts ...ClientOption) *Client

NewClient 创建一个新的 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) SetInsecure

func (c *Client) SetInsecure(insecure bool)

SetInsecure 动态设置是否允许不安全访问

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 WithBaseURL

func WithBaseURL(baseURL string) ClientOption

WithBaseURL 设置基础 URL

func WithBasicAuth

func WithBasicAuth(username, password string) ClientOption

WithBasicAuth 设置基本认证

func WithClientTimeout

func WithClientTimeout(timeout time.Duration) ClientOption

WithClientTimeout 设置请求超时时间

func WithDebug

func WithDebug(debug bool) ClientOption

WithDebug 启用调试模式,输出详细的请求和响应信息

func WithHeaders

func WithHeaders(headers map[string]string) ClientOption

WithHeaders 设置默认请求头

func WithInsecure

func WithInsecure(insecure bool) ClientOption

WithInsecure 允许不安全的 HTTPS 访问(跳过 SSL/TLS 证书验证) 注意:仅在开发或测试环境中使用,生产环境不推荐

func WithResponseInterceptor

func WithResponseInterceptor(interceptor ResponseInterceptor) ClientOption

WithResponseInterceptor 设置响应拦截器,在每次 HTTP 响应返回前调用

func WithRetries

func WithRetries(retries int) ClientOption

WithRetries 设置重试次数

func WithUserAgent

func WithUserAgent(userAgent string) ClientOption

WithUserAgent 设置 User-Agent

type ErrorType

type ErrorType int

ErrorType 定义 HTTP 错误的类型

const (
	// ErrorTypeNetwork 网络错误(连接失败、超时等)
	ErrorTypeNetwork ErrorType = iota
	// ErrorTypeHTTP HTTP 错误(非 2xx 状态码)
	ErrorTypeHTTP
	// ErrorTypeParse 解析错误(JSON 反序列化失败)
	ErrorTypeParse
)

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) Error

func (e *HttpError) Error() string

Error 实现 error 接口

func (*HttpError) IsHTTPError

func (e *HttpError) IsHTTPError() bool

IsHTTPError 检查是否是 HTTP 错误

func (*HttpError) IsNetworkError

func (e *HttpError) IsNetworkError() bool

IsNetworkError 检查是否是网络错误

func (*HttpError) IsParseError

func (e *HttpError) IsParseError() bool

IsParseError 检查是否是解析错误

type RequestOption

type RequestOption func(*resty.Request)

RequestOption 是单个请求的配置选项

func WithBody

func WithBody(body interface{}) RequestOption

WithBody 设置请求体

func WithFormData

func WithFormData(data map[string]string) RequestOption

WithFormData 设置表单数据

func WithHeader

func WithHeader(key, value string) RequestOption

WithHeader 为单个请求添加请求头

func WithPathParam

func WithPathParam(key, value string) RequestOption

WithPathParam 设置路径参数

func WithPathParams

func WithPathParams(params map[string]string) RequestOption

WithPathParams 设置多个路径参数

func WithQueryParam

func WithQueryParam(key, value string) RequestOption

WithQueryParam 添加查询参数

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 是响应的包装器

func (*Response) Into

func (r *Response) Into(v interface{}) error

Into 将响应体反序列化为指定的结构体类型

func (*Response) IntoJSON

func (r *Response) IntoJSON() (map[string]interface{}, error)

IntoJSON 将响应体反序列化为 JSON(map[string]interface{})

func (*Response) IsError

func (r *Response) IsError() bool

IsError 检查响应是否出错

func (*Response) IsSuccess

func (r *Response) IsSuccess() bool

IsSuccess 检查响应是否成功(状态码 2xx)

func (*Response) String

func (r *Response) String() string

String 返回响应体的字符串表示

type ResponseInterceptor

type ResponseInterceptor func(resp *Response) *Response

ResponseInterceptor 响应拦截器,在 HTTP 方法返回 Response 之前调用。 可以修改或替换 Response(如注入错误消息)。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL