Documentation
¶
Overview ¶
* @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\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: 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: 2024-11-29 18:49:55 * @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: 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
- func DecodeRespBody(resp *Response, dst any) error
- func IsValidMethod(method string) bool
- func ReadAndCacheResponseBody(resp *http.Response) (string, error)
- func SetTimeout(timeout time.Duration) *http.Client
- type BodyEncodeFunc
- type Client
- func NewClientWithContext(client *http.Client, ctx context.Context) *Client
- func NewCustomDefaultClient() *Client
- func NewCustomDefaultClientWithContext(ctx context.Context) *Client
- func NewDefaultHttpClient() *Client
- func NewDefaultHttpClientWithContext(ctx context.Context) *Client
- func NewHttpClient(client *http.Client) *Client
- func (c *Client) Connect(endpoint string) *Request
- func (c *Client) Delete(endpoint string) *Request
- func (c *Client) Get(endpoint string) *Request
- func (c *Client) Head(endpoint string) *Request
- func (c *Client) NewRequest(method, endpoint string) *Request
- func (c *Client) Options(endpoint string) *Request
- func (c *Client) Patch(endpoint string) *Request
- func (c *Client) Post(endpoint string) *Request
- func (c *Client) Put(endpoint string) *Request
- func (c *Client) Trace(endpoint string) *Request
- type Request
- func (r *Request) AddHeader(key, value string) *Request
- func (r *Request) AddQuery(key, value string) *Request
- func (r *Request) GetBody() any
- func (r *Request) GetBodyBytes() io.Reader
- func (r *Request) GetBodyEncodeFunc() BodyEncodeFunc
- func (r *Request) GetClient() *http.Client
- func (r *Request) GetCtx() context.Context
- func (r *Request) GetError() error
- func (r *Request) GetHeaders() http.Header
- func (r *Request) GetMethod() string
- func (r *Request) GetQueryValues() url.Values
- func (r *Request) GetURL() string
- func (r *Request) Send() (Response, error)
- func (r *Request) SetBody(body any) *Request
- func (r *Request) SetBodyEncodeFunc(fn BodyEncodeFunc) *Request
- func (r *Request) SetBodyForm(data url.Values) *Request
- func (r *Request) SetBodyMultipart(fieldName, fileName string, fileContent []byte) *Request
- func (r *Request) SetHeader(key, value string) *Request
- func (r *Request) SetQuery(key, value string) *Request
- type Response
Constants ¶
const ( ErrUnsupportedContentType errorx.ErrorType = iota // 不支持的内容类型 ErrInvalidMethod // 无效的请求方法 ErrBodyEncodeFuncNotSet // 请求体编码函数未设置 ErrExpectedDestinationType // 期望的目标类型不匹配 ErrRequestStatusCode // 请求状态码错误 )
const ( HeaderContentType = "Content-Type" // HTTP 请求和响应头中的 Content-Type // 常见的 Content-Type ContentTypeTextPlain = "text/plain" // 纯文本格式 ContentTypeTextPlainCharacterUTF8 = "text/plain; charset=utf-8" // UTF-8 编码的纯文本 ContentTypeApplicationJSON = "application/json" // JSON 格式 ContentTypeApplicationJSONCharacterUTF8 = "application/json; charset=utf-8" // UTF-8 编码的 JSON ContentTypeApplicationXML = "application/xml" // XML 格式 ContentTypeApplicationXMLCharacterUTF8 = "application/xml; charset=utf-8" // UTF-8 编码的 XML ContentTypeTextXML = "text/xml" // XML 文本格式 ContentTypeTextXMLCharacterUTF8 = "text/xml; charset=utf-8" // UTF-8 编码的 XML 文本 ContentTypeApplicationOctetStream = "application/octet-stream" // 二进制流格式 ContentTypeMultipartFormData = "multipart/form-data" // 表单数据格式 ContentTypeWWWFormURLEncoded = "application/x-www-form-urlencoded" // URL 编码的表单数据 )
HTTP 请求和响应相关的常量定义
Variables ¶
This section is empty.
Functions ¶
func DecodeRespBody ¶
DecodeRespBody 根据响应的 Content-Type 解码响应体
func ReadAndCacheResponseBody ¶
ReadAndCacheResponseBody 读取并缓存响应体
Types ¶
type BodyEncodeFunc ¶
BodyEncodeFunc 定义请求体编码函数类型 该函数接收任意类型的请求体并返回 io.Reader 和可能的错误
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client 是一个封装 http.Client 的结构体
func NewClientWithContext ¶
NewClientWithContext 创建一个使用自定义 http.Client 的 Client 实例
func NewCustomDefaultClient ¶
func NewCustomDefaultClient() *Client
NewCustomDefaultClient 创建一个使用自定义的 http.Client 的 Client 实例
func NewCustomDefaultClientWithContext ¶
NewCustomDefaultClientWithContext 创建一个使用自定义的 http.Client 和自定义上下文的 Client 实例
func NewDefaultHttpClient ¶
func NewDefaultHttpClient() *Client
NewDefaultHttpClient 创建一个使用默认 http.Client 的 Client 实例
func NewDefaultHttpClientWithContext ¶
NewDefaultHttpClientWithContext 创建一个使用默认 http.Client 和自定义上下文的 Client 实例
func NewHttpClient ¶
NewHttpClient 创建一个使用自定义 http.Client 的 Client 实例
func (*Client) NewRequest ¶
NewRequest 创建一个新的 Request 实例
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
Request 结构体用于封装 HTTP 请求的相关信息
func NewRequest ¶
NewRequest 创建一个新的 HTTP 请求
func (*Request) GetBodyEncodeFunc ¶
func (r *Request) GetBodyEncodeFunc() BodyEncodeFunc
GetBodyEncodeFunc 返回自定义的请求体编码函数
func (*Request) GetQueryValues ¶
GetQueryValues 返回查询参数
func (*Request) SetBodyEncodeFunc ¶
func (r *Request) SetBodyEncodeFunc(fn BodyEncodeFunc) *Request
SetBodyEncodeFunc 设置请求体编码函数
func (*Request) SetBodyForm ¶
SetBodyForm 设置请求体为表单数据
func (*Request) SetBodyMultipart ¶
SetBodyMultipart 设置请求体为 multipart/form-data
type Response ¶
Response 结构体用于封装 HTTP 响应
func (*Response) DecodeRespBody ¶
DecodeRespBody 解码响应体到目标结构体