httpx

package
v0.11.36 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

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

View Source
const (
	ErrUnsupportedContentType  errorx.ErrorType = iota // 不支持的内容类型
	ErrInvalidMethod                                   // 无效的请求方法
	ErrBodyEncodeFuncNotSet                            // 请求体编码函数未设置
	ErrExpectedDestinationType                         // 期望的目标类型不匹配
	ErrRequestStatusCode                               // 请求状态码错误
)
View Source
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

func DecodeRespBody(resp *Response, dst any) error

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

func IsValidMethod

func IsValidMethod(method string) bool

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

func ReadAndCacheResponseBody

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

ReadAndCacheResponseBody 读取并缓存响应体

func SetTimeout

func SetTimeout(timeout time.Duration) *http.Client

SetTimeout 设置 HTTP 客户端的超时时间

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 NewClientWithContext

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

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

func NewCustomDefaultClient

func NewCustomDefaultClient() *Client

NewCustomDefaultClient 创建一个使用自定义的 http.Client 的 Client 实例

func NewCustomDefaultClientWithContext

func NewCustomDefaultClientWithContext(ctx context.Context) *Client

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

func NewDefaultHttpClient

func NewDefaultHttpClient() *Client

NewDefaultHttpClient 创建一个使用默认 http.Client 的 Client 实例

func NewDefaultHttpClientWithContext

func NewDefaultHttpClientWithContext(ctx context.Context) *Client

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

func NewHttpClient

func NewHttpClient(client *http.Client) *Client

NewHttpClient 创建一个使用自定义 http.Client 的 Client 实例

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

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

AddHeader 添加请求头

func (*Request) AddQuery

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

AddQuery 添加查询参数

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 客户端

func (*Request) GetCtx

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

GetCtx 返回请求的上下文

func (*Request) GetError

func (r *Request) GetError() error

GetError 返回错误信息

func (*Request) GetHeaders

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

GetHeaders 返回请求头

func (*Request) GetMethod

func (r *Request) GetMethod() string

GetMethod 返回请求的方法

func (*Request) GetQueryValues

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

GetQueryValues 返回查询参数

func (*Request) GetURL

func (r *Request) GetURL() string

GetURL 返回请求的 URL

func (*Request) Send

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

Send 执行 HTTP 请求

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

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

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

func (*Request) SetHeader

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

SetHeader 设置请求头

func (*Request) SetQuery

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

SetQuery 设置查询参数

type Response

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

Response 结构体用于封装 HTTP 响应

func (*Response) CheckStatus

func (r *Response) CheckStatus() error

CheckStatus 检查响应状态码

func (*Response) Close

func (r *Response) Close() error

Close 关闭 HTTP 响应体

func (*Response) DecodeRespBody

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

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

func (*Response) GetBody

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

GetBody 读取响应体

func (*Response) GetError

func (r *Response) GetError() error

GetError 返回错误信息

func (*Response) IsError

func (r *Response) IsError() bool

IsError 检查响应是否有错误

func (*Response) LogResponse

func (r *Response) LogResponse()

LogResponse 日志记录响应信息

Jump to

Keyboard shortcuts

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