httpx

package
v0.19.4 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

* @Author: kamalyes 501893067@qq.com * @Date: 2024-11-28 18:55:55 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-08-20 16:33:01 * @FilePath: \go-toolbox\pkg\httpx\client.go * @Description: HTTP 客户端实现 * * Copyright (c) 2024 by kamalyes, All Rights Reserved.

* @Author: kamalyes 501893067@qq.com * @Date: 2024-11-28 18:55:55 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2024-11-30 15:26:55 * @FilePath: \go-toolbox\pkg\httpx\error.go * @Description: HTTP 相关错误定义 * * Copyright (c) 2024 by kamalyes, All Rights Reserved.

* @Author: kamalyes 501893067@qq.com * @Date: 2026-01-05 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2026-01-05 11:32:06 * @FilePath: \go-toolbox\pkg\httpx\helpers.go * @Description: HTTP 辅助方法 * * Copyright (c) 2026 by kamalyes, All Rights Reserved.

* @Author: kamalyes 501893067@qq.com * @Date: 2026-01-16 13:30:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2026-01-16 21:11:58 * @FilePath: \go-toolbox\pkg\httpx\params.go * @Description: 参数构建器 * * Copyright (c) 2026 by kamalyes, All Rights Reserved.

* @Author: kamalyes 501893067@qq.com * @Date: 2024-11-28 18:55:55 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2024-11-30 20:17:08 * @FilePath: \go-toolbox\pkg\httpx\request.go * @Description: HTTP 请求封装 * * Copyright (c) 2024 by kamalyes, All Rights Reserved.

* @Author: kamalyes 501893067@qq.com * @Date: 2024-11-28 18:55:55 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-05-27 15:05:08 * @FilePath: \go-toolbox\pkg\httpx\response.go * @Description: HTTP 响应封装 * * Copyright (c) 2024 by kamalyes, All Rights Reserved.

* @Author: kamalyes 501893067@qq.com * @Date: 2024-11-28 18:55:55 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2024-11-28 18:55:55 * @FilePath: \go-toolbox\pkg\httpx\types.go * @Description: HTTP 相关常量和类型定义 * * Copyright (c) 2024 by kamalyes, All Rights Reserved.

* @Author: kamalyes 501893067@qq.com * @Date: 2026-01-16 21:50:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2026-01-16 21:50:00 * @FilePath: \go-toolbox\pkg\httpx\url.go * @Description: URL 处理工具函数 * * Copyright (c) 2026 by kamalyes, All Rights Reserved.

* @Author: kamalyes 501893067@qq.com * @Date: 2024-11-28 18:55:55 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2024-11-30 20:17:08 * @FilePath: \go-toolbox\pkg\httpx\validator.go * @Description: * * Copyright (c) 2024 by kamalyes, All Rights Reserved.

Index

Constants

View Source
const (
	ErrUnsupportedContentType  errorx.ErrorType = 30000 + iota // 不支持的内容类型
	ErrInvalidMethod                                           // 无效的请求方法
	ErrBodyEncodeFuncNotSet                                    // 请求体编码函数未设置
	ErrExpectedDestinationType                                 // 期望的目标类型不匹配
	ErrRequestStatusCode                                       // 请求状态码错误
)
View Source
const (
	// HTTP Headers
	HeaderContentType   = "Content-Type"  // HTTP 请求和响应头中的 Content-Type
	HeaderAccept        = "Accept"        // HTTP 请求头中的 Accept
	HeaderAuthorization = "Authorization" // HTTP 请求头中的 Authorization
	HeaderUserAgent     = "User-Agent"    // HTTP 请求头中的 User-Agent
	HeaderCookie        = "Cookie"        // HTTP 请求头中的 Cookie

	// 常见的 Content-Type
	ContentTypeTextPlain                    = "text/plain"                        // 纯文本格式
	ContentTypeTextPlainCharacterUTF8       = "text/plain; charset=utf-8"         // UTF-8 编码的纯文本
	ContentTypeApplicationJSON              = "application/json"                  // JSON 格式
	ContentTypeApplicationXNDJSON           = "application/x-ndjson"              // NDJSON 格式
	ContentTypeApplicationXSnappyFramed     = "application/x-snappy-framed"       // Snappy 压缩格式的 JSON
	ContentTypeApplicationJSONCharacterUTF8 = "application/json; charset=utf-8"   // UTF-8 编码的 JSON
	ContentTypeApplicationXML               = "application/xml"                   // XML 格式
	ContentTypeApplicationXMLCharacterUTF8  = "application/xml; charset=utf-8"    // UTF-8 编码的 XML
	ContentTypeTextHTML                     = "text/html"                         // HTML 文本格式
	ContentTypeTextHTMLCharacterUTF8        = "text/html; charset=utf-8"          // UTF-8 编码的 HTML 文本
	ContentTypeTextXML                      = "text/xml"                          // XML 文本格式
	ContentTypeTextXMLCharacterUTF8         = "text/xml; charset=utf-8"           // UTF-8 编码的 XML 文本
	ContentTypeTextCSV                      = "text/csv"                          // CSV 文本格式
	ContentTypeTextCSVCharacterUTF8         = "text/csv; charset=utf-8"           // UTF-8 编码的 CSV 文本
	ContentTypeImageJPEG                    = "image/jpeg"                        // JPEG 图片格式
	ContentTypeImagePNG                     = "image/png"                         // PNG 图片格式
	ContentTypeImageGIF                     = "image/gif"                         // GIF 图片格式
	ContentTypeImageWebP                    = "image/webp"                        // WebP 图片格式
	ContentTypeApplicationOctetStream       = "application/octet-stream"          // 二进制流格式
	ContentTypeMultipartFormData            = "multipart/form-data"               // 表单数据格式
	ContentTypeWWWFormURLEncoded            = "application/x-www-form-urlencoded" // URL 编码的表单数据
)

HTTP 请求和响应相关的常量定义

Variables

This section is empty.

Functions

func BuildParams

func BuildParams(base map[string]string, opts ...func(map[string]string)) map[string]string

BuildParams 构建请求参数的辅助方法 基础参数通过 base 传入,可选参数通过 opts 传入

func DecodeRespBody

func DecodeRespBody(resp *Response, dst any) error

DecodeRespBody 根据响应的 Content-Type 解码响应体

func GetCookies

func GetCookies(url string) ([]*http.Cookie, error)

func GetRequestValue

func GetRequestValue(r *http.Request, contextKey interface{}, headerName, queryName string) string

GetRequestValue 从 HTTP 请求中获取指定值

查找顺序: 1. 优先从上下文(Context)获取 2. 然后从请求头(Header)获取 3. 最后从查询参数(Query)获取

参数:

  • r: HTTP 请求对象
  • contextKey: 上下文中存储的键(可为 nil)
  • headerName: 请求头中的字段名
  • queryName: 查询参数中的字段名

返回:找到的值,未找到则返回空字符串

func GetValueFromHeaderOrQuery

func GetValueFromHeaderOrQuery(r *http.Request, headerName, queryName string) string

GetValueFromHeaderOrQuery 从请求头或查询参数中获取值

查找顺序: 1. 优先从请求头(Header)获取 2. 然后从查询参数(Query)获取

常用于获取签名、时间戳、Nonce 等可能在 Header 或 Query 中的参数

参数:

  • r: HTTP 请求对象
  • headerName: 请求头中的字段名
  • queryName: 查询参数中的字段名

返回:找到的值,未找到则返回空字符串

func IsValidMethod

func IsValidMethod(method string) bool

IsValidMethod 校验传入的 HTTP 方法是否有效

func NormalizeBaseURL

func NormalizeBaseURL(baseURL string) string

NormalizeBaseURL 规范化基础 URL,确保包含协议前缀 如果 URL 不包含 http:// 或 https:// 协议,则自动添加 https:// 参数:

  • baseURL: 待规范化的基础 URL

返回:

  • 规范化后的 URL,确保包含协议前缀

示例:

NormalizeBaseURL("www.example.com")          // 返回 "https://www.example.com"
NormalizeBaseURL("example.com/api")          // 返回 "https://example.com/api"
NormalizeBaseURL("http://example.com")       // 返回 "http://example.com"
NormalizeBaseURL("https://example.com")      // 返回 "https://example.com"
NormalizeBaseURL("")                         // 返回 ""

func ReadAndCacheResponseBody

func ReadAndCacheResponseBody(resp *http.Response) (string, error)

ReadAndCacheResponseBody 读取并缓存响应体

func ReadRequestBody

func ReadRequestBody(r *http.Request) ([]byte, error)

ReadRequestBody 读取 HTTP 请求体(支持重复读取)

读取后会重新设置请求体,使其可以被后续处理器再次读取 这对于需要多次访问请求体的中间件(如签名验证、日志记录)非常有用

参数:

  • r: HTTP 请求对象

返回:

  • 请求体内容(字节数组)
  • 读取错误(如果有)

注意:如果请求体为 nil,返回 (nil, nil)

func WithParam

func WithParam(condition bool, key, value string) func(map[string]string)

WithParam 条件添加参数

func WithParamNotEmpty

func WithParamNotEmpty(key, value string) func(map[string]string)

WithParamNotEmpty 非空时添加参数

Types

type BodyEncodeFunc

type BodyEncodeFunc func(body any) (io.Reader, error)

BodyEncodeFunc 定义请求体编码函数类型 该函数接收任意类型的请求体并返回 io.Reader 和可能的错误

type Client

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

Client 是一个封装 http.Client 的结构体

func NewClient

func NewClient(opts ...ClientOption) *Client

NewClient 创建一个新的 HTTP 客户端(支持函数式选项)

func NewClientWithContext

func NewClientWithContext(client *http.Client, ctx context.Context) *Client

NewClientWithContext 创建一个使用自定义 http.Client 和自定义上下文的 Client 实例

func NewCustomDefaultClient

func NewCustomDefaultClient() *Client

NewCustomDefaultClient 创建一个使用自定义配置的 http.Client 的 Client 实例,默认上下文为 context.Background()

func NewCustomDefaultClientWithContext

func NewCustomDefaultClientWithContext(ctx context.Context) *Client

NewCustomDefaultClientWithContext 创建一个使用自定义配置的 http.Client 和自定义上下文的 Client 实例

func NewDefaultHttpClient

func NewDefaultHttpClient() *Client

NewDefaultHttpClient 创建一个使用默认 http.Client 的 Client 实例,默认上下文为 context.Background()

func NewDefaultHttpClientWithContext

func NewDefaultHttpClientWithContext(ctx context.Context) *Client

NewDefaultHttpClientWithContext 创建一个使用默认 http.Client 和自定义上下文的 Client 实例

func NewHttpClient

func NewHttpClient(client *http.Client) *Client

NewHttpClient 创建一个使用自定义 http.Client 的 Client 实例,默认上下文为 context.Background()

func (*Client) Connect

func (c *Client) Connect(endpoint string) *Request

Connect 创建一个 CONNECT 请求

func (*Client) Delete

func (c *Client) Delete(endpoint string) *Request

Delete 创建一个 DELETE 请求

func (*Client) Get

func (c *Client) Get(endpoint string) *Request

Get 创建一个 GET 请求

func (*Client) Head

func (c *Client) Head(endpoint string) *Request

Head 创建一个 HEAD 请求

func (*Client) NewRequest

func (c *Client) NewRequest(method, endpoint string) *Request

NewRequest 创建一个新的 HTTP 请求封装 Request 实例

func (*Client) Options

func (c *Client) Options(endpoint string) *Request

Options 创建一个 OPTIONS 请求

func (*Client) Patch

func (c *Client) Patch(endpoint string) *Request

Patch 创建一个 PATCH 请求

func (*Client) Post

func (c *Client) Post(endpoint string) *Request

Post 创建一个 POST 请求

func (*Client) Put

func (c *Client) Put(endpoint string) *Request

Put 创建一个 PUT 请求

func (*Client) Trace

func (c *Client) Trace(endpoint string) *Request

Trace 创建一个 TRACE 请求

type ClientOption

type ClientOption func(*clientConfig)

ClientOption 客户端选项函数

func WithContext

func WithContext(ctx context.Context) ClientOption

WithContext 设置请求上下文

func WithIdleConnTimeout

func WithIdleConnTimeout(timeout time.Duration) ClientOption

WithIdleConnTimeout 设置空闲连接超时时间

func WithInsecureSkipVerify

func WithInsecureSkipVerify(skip bool) ClientOption

WithInsecureSkipVerify 设置是否跳过 TLS 证书验证(生产环境慎用)

func WithMaxConnsPerHost

func WithMaxConnsPerHost(n int) ClientOption

WithMaxConnsPerHost 设置每个主机最大连接数

func WithMaxIdleConns

func WithMaxIdleConns(n int) ClientOption

WithMaxIdleConns 设置最大空闲连接数

func WithMaxIdleConnsPerHost

func WithMaxIdleConnsPerHost(n int) ClientOption

WithMaxIdleConnsPerHost 设置每个主机最大空闲连接数

func WithTLSHandshakeTimeout

func WithTLSHandshakeTimeout(timeout time.Duration) ClientOption

WithTLSHandshakeTimeout 设置 TLS 握手超时时间

func WithTimeout

func WithTimeout(timeout time.Duration) ClientOption

WithTimeout 设置请求超时时间

type FileField

type FileField struct {
	FileName string
	Content  []byte
}

FileField 文件字段结构

type ParamsBuilder

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

ParamsBuilder 参数构建器,支持链式调用

func NewParams

func NewParams() *ParamsBuilder

NewParams 创建新的参数构建器

func NewParamsWithBase

func NewParamsWithBase(base map[string]string) *ParamsBuilder

NewParamsWithBase 创建带基础参数的构建器

func (*ParamsBuilder) Add

func (b *ParamsBuilder) Add(key, value string) *ParamsBuilder

Add 设置参数(无条件,别名)

func (*ParamsBuilder) Build

func (b *ParamsBuilder) Build() map[string]string

Build 构建并返回参数 map

func (*ParamsBuilder) Clear

func (b *ParamsBuilder) Clear() *ParamsBuilder

Clear 清空所有参数

func (*ParamsBuilder) Clone

func (b *ParamsBuilder) Clone() *ParamsBuilder

Clone 克隆参数构建器

func (*ParamsBuilder) Delete

func (b *ParamsBuilder) Delete(key string) *ParamsBuilder

Delete 删除参数

func (*ParamsBuilder) Get

func (b *ParamsBuilder) Get(key string) (string, bool)

Get 获取参数值

func (*ParamsBuilder) Has

func (b *ParamsBuilder) Has(key string) bool

Has 检查参数是否存在

func (*ParamsBuilder) Keys

func (b *ParamsBuilder) Keys() []string

Keys 返回所有参数的键

func (*ParamsBuilder) Len

func (b *ParamsBuilder) Len() int

Len 返回参数数量

func (*ParamsBuilder) Merge

func (b *ParamsBuilder) Merge(other *ParamsBuilder) *ParamsBuilder

Merge 合并另一个参数构建器的参数

func (*ParamsBuilder) Set

func (b *ParamsBuilder) Set(key, value string) *ParamsBuilder

Set 设置参数(无条件)

func (*ParamsBuilder) SetAny

func (b *ParamsBuilder) SetAny(key string, value interface{}) *ParamsBuilder

SetAny 设置任意类型的参数值(会转换为字符串)

func (*ParamsBuilder) SetAnyIf

func (b *ParamsBuilder) SetAnyIf(condition bool, key string, value interface{}) *ParamsBuilder

SetAnyIf 条件设置任意类型的参数

func (*ParamsBuilder) SetIf

func (b *ParamsBuilder) SetIf(condition bool, key, value string) *ParamsBuilder

SetIf 条件设置参数

func (*ParamsBuilder) SetMultiple

func (b *ParamsBuilder) SetMultiple(params map[string]string) *ParamsBuilder

SetMultiple 批量设置参数

func (*ParamsBuilder) SetNotEmpty

func (b *ParamsBuilder) SetNotEmpty(key, value string) *ParamsBuilder

SetNotEmpty 非空时设置参数(使用 validator.IsEmptyValue 判断)

func (*ParamsBuilder) ToSlice

func (b *ParamsBuilder) ToSlice() []interface{}

ToSlice 将参数转换为 []interface{} 格式,用于日志记录 返回格式: []interface{}{"key1", "value1", "key2", "value2", ...}

func (*ParamsBuilder) Values

func (b *ParamsBuilder) Values() []string

Values 返回所有参数的值

type Request

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

Request 结构体用于封装 HTTP 请求的相关信息

func NewRequest

func NewRequest(ctx context.Context, client *http.Client, method, endpoint string) *Request

NewRequest 创建一个新的 HTTP 请求

func (*Request) AddCookie

func (r *Request) AddCookie(cookie *http.Cookie) *Request

AddCookie 添加 Cookie 到请求(标准方法,与 net/http.Request 一致)

func (*Request) AddHeader

func (r *Request) AddHeader(key, value string) *Request

AddHeader 添加请求头

func (*Request) AddHeaders

func (r *Request) AddHeaders(headers map[string]string) *Request

AddHeaders 批量添加请求头

func (*Request) AddQueries

func (r *Request) AddQueries(queries map[string]string) *Request

AddQueries 批量添加查询参数

func (*Request) AddQuery

func (r *Request) AddQuery(key, value string) *Request

AddQuery 添加查询参数

func (*Request) Client

func (r *Request) Client() *http.Client

Client 返回 HTTP 客户端

func (*Request) Clone

func (r *Request) Clone() *Request

Clone 克隆请求(用于重试等场景)

func (*Request) Context

func (r *Request) Context() context.Context

Context 返回请求的上下文(标准方法,与 net/http 保持一致)

func (*Request) Cookie

func (r *Request) Cookie(name string) (*http.Cookie, error)

Cookie 获取指定名称的 Cookie(标准方法,与 net/http.Request 一致)

func (*Request) Do

func (r *Request) Do(ctx context.Context) ([]byte, error)

Do 执行 HTTP 请求并返回响应字节数据(简化版本,用于快速获取响应体)

func (*Request) FullURL

func (r *Request) FullURL() string

FullURL 返回包含查询参数的完整 URL(标准方法)

func (*Request) GetBody

func (r *Request) GetBody() any

GetBody 返回请求体

func (*Request) GetBodyBytes

func (r *Request) GetBodyBytes() io.Reader

GetBodyBytes 返回请求体的字节流

func (*Request) GetBodyEncodeFunc

func (r *Request) GetBodyEncodeFunc() BodyEncodeFunc

GetBodyEncodeFunc 返回自定义的请求体编码函数

func (*Request) GetClient

func (r *Request) GetClient() *http.Client

GetClient 返回 HTTP 客户端 Deprecated: 使用 Client() 代替

func (*Request) GetCtx

func (r *Request) GetCtx() context.Context

GetCtx 返回请求的上下文 Deprecated: 使用 Context() 代替

func (*Request) GetError

func (r *Request) GetError() error

GetError 返回错误信息

func (*Request) GetFullURL

func (r *Request) GetFullURL() string

GetFullURL 返回包含查询参数的完整 URL Deprecated: 使用 FullURL() 代替

func (*Request) GetHeaders

func (r *Request) GetHeaders() http.Header

GetHeaders 返回请求头

func (*Request) GetMethod

func (r *Request) GetMethod() string

GetMethod 返回请求的方法 Deprecated: 使用 Method() 代替

func (*Request) GetQueryValues

func (r *Request) GetQueryValues() url.Values

GetQueryValues 返回查询参数

func (*Request) GetURL

func (r *Request) GetURL() string

GetURL 返回请求的 URL Deprecated: 使用 URL() 代替

func (*Request) Header

func (r *Request) Header() http.Header

Header 返回请求头(标准方法,与 net/http.Request 一致)

func (*Request) Method

func (r *Request) Method() string

Method 返回请求的方法(标准方法)

func (*Request) MustSend

func (r *Request) MustSend() Response

MustSend 执行 HTTP 请求,如果失败则 panic

func (*Request) Send

func (r *Request) Send() (Response, error)

Send 执行 HTTP 请求

func (*Request) SetAccept

func (r *Request) SetAccept(accept string) *Request

SetAccept 设置 Accept

func (*Request) SetAuthorization

func (r *Request) SetAuthorization(token string) *Request

SetAuthorization 设置 Authorization

func (*Request) SetBearerToken

func (r *Request) SetBearerToken(token string) *Request

SetBearerToken 设置 Bearer Token

func (*Request) SetBody

func (r *Request) SetBody(body any) *Request

SetBody 设置请求体

func (*Request) SetBodyEncodeFunc

func (r *Request) SetBodyEncodeFunc(fn BodyEncodeFunc) *Request

SetBodyEncodeFunc 设置请求体编码函数

func (*Request) SetBodyForm

func (r *Request) SetBodyForm(data url.Values) *Request

SetBodyForm 设置请求体为表单数据

func (*Request) SetBodyJSON

func (r *Request) SetBodyJSON(body any) *Request

SetBodyJSON 设置 JSON 请求体(自动设置 Content-Type)

func (*Request) SetBodyMultipart

func (r *Request) SetBodyMultipart(fieldName, fileName string, fileContent []byte) *Request

SetBodyMultipart 设置请求体为 multipart/form-data

func (*Request) SetBodyMultipartWithFields

func (r *Request) SetBodyMultipartWithFields(fields map[string]string, files map[string]FileField) *Request

SetBodyMultipartWithFields 设置请求体为 multipart/form-data(支持多个字段)

func (*Request) SetBodyRaw

func (r *Request) SetBodyRaw(body []byte) *Request

SetBodyRaw 设置原始请求体,不进行任何编码

func (*Request) SetBodyString

func (r *Request) SetBodyString(body string) *Request

SetBodyString 设置字符串请求体

func (*Request) SetContentType

func (r *Request) SetContentType(contentType string) *Request

SetContentType 设置 Content-Type

func (*Request) SetEndpoint

func (r *Request) SetEndpoint(endpoint string) *Request

SetEndpoint 设置Endpoint

func (*Request) SetHeader

func (r *Request) SetHeader(key, value string) *Request

SetHeader 设置请求头

func (*Request) SetHeaders

func (r *Request) SetHeaders(headers map[string]string) *Request

SetHeaders 批量设置请求头

func (*Request) SetQueries

func (r *Request) SetQueries(queries map[string]string) *Request

SetQueries 批量设置查询参数

func (*Request) SetQuery

func (r *Request) SetQuery(key, value string) *Request

SetQuery 设置查询参数

func (*Request) SetQueryValues

func (r *Request) SetQueryValues(values url.Values) *Request

SetBodyyValues 直接设置 url.Values

func (*Request) SetUserAgent

func (r *Request) SetUserAgent(userAgent string) *Request

SetUserAgent 设置 User-Agent

func (*Request) URL

func (r *Request) URL() string

URL 返回请求的 URL(标准方法)

func (*Request) WithContext

func (r *Request) WithContext(ctx context.Context) *Request

WithContext 返回一个使用新上下文的请求副本

type Response

type Response struct {
	*http.Response // 原始 HTTP 响应
	// contains filtered or unexported fields
}

Response 结构体用于封装 HTTP 响应

func (*Response) Body

func (r *Response) Body() ([]byte, error)

Body 读取响应体(标准方法,与 net/http 保持一致)

func (*Response) Bytes

func (r *Response) Bytes() ([]byte, error)

Bytes 读取响应体(Body 的别名)

func (*Response) CheckStatus

func (r *Response) CheckStatus() error

CheckStatus 检查响应状态码

func (*Response) Close

func (r *Response) Close() error

Close 关闭 HTTP 响应体

func (*Response) Decode

func (r *Response) Decode(dst any) error

Decode 根据 Content-Type 自动解码响应体

func (*Response) DecodeRespBody

func (r *Response) DecodeRespBody(dst any) error

DecodeRespBody 解码响应体到目标结构体 Deprecated: 使用 JSON()、XML() 或 Decode() 代替

func (*Response) Error

func (r *Response) Error() error

Error 返回错误信息(标准方法)

func (*Response) GetBody

func (r *Response) GetBody() ([]byte, error)

GetBody 读取响应体 Deprecated: 使用 Body() 或 Bytes() 代替

func (*Response) GetCookies

func (r *Response) GetCookies() ([]*http.Cookie, error)

GetCookies 获取响应中的 Cookie(保留兼容性)

func (*Response) GetError

func (r *Response) GetError() error

GetError 返回错误信息 Deprecated: 使用 Error() 代替

func (*Response) IsError

func (r *Response) IsError() bool

IsError 检查响应是否有错误

func (*Response) JSON

func (r *Response) JSON(dst any) error

JSON 解码 JSON 响应体到目标结构体

func (*Response) LogResponse

func (r *Response) LogResponse()

LogResponse 日志记录响应信息

func (*Response) OK

func (r *Response) OK() bool

OK 检查响应状态码是否为 200

func (*Response) String

func (r *Response) String() (string, error)

String 读取响应体并转换为字符串

func (*Response) XML

func (r *Response) XML(dst any) error

XML 解码 XML 响应体到目标结构体

Jump to

Keyboard shortcuts

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