http_utils

package
v1.1.15 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Logger *log.Logger

Functions

This section is empty.

Types

type HTTPClient

type HTTPClient struct {
	Client *http.Client
}

HTTPClient HTTP客户端封装

func NewHTTPClient

func NewHTTPClient() *HTTPClient

NewHTTPClient 创建新的HTTP客户端

func (*HTTPClient) Do added in v1.1.4

func (c *HTTPClient) Do(req *http.Request) *HTTPResult

Do 执行HTTP请求

func (*HTTPClient) EnableConnectionPool added in v1.1.4

func (c *HTTPClient) EnableConnectionPool(maxIdle, maxIdlePerHost int)

EnableConnectionPool 启用连接池

func (*HTTPClient) Get added in v1.1.4

func (c *HTTPClient) Get(url string, headers map[string]string, queryParams map[string]string) *HTTPResult

Get 执行GET请求

func (*HTTPClient) Post added in v1.1.4

func (c *HTTPClient) Post(url string, headers map[string]string) *HTTPResult

Post 执行POST请求

func (*HTTPClient) PostForm added in v1.1.4

func (c *HTTPClient) PostForm(url string, formData map[string]string, headers map[string]string) *HTTPResult

PostForm 执行表单POST请求

func (*HTTPClient) PostJSON added in v1.1.4

func (c *HTTPClient) PostJSON(url string, body interface{}, headers map[string]string) *HTTPResult

PostJSON 执行POST JSON请求

func (*HTTPClient) PostMultipart added in v1.1.4

func (c *HTTPClient) PostMultipart(url string, formData map[string]string, files map[string]string, headers map[string]string) *HTTPResult

PostMultipart 执行multipart/form-data POST请求

func (*HTTPClient) SendRequest

func (c *HTTPClient) SendRequest(request *HTTPRequest) *HTTPResult

SendRequest 执行HTTP请求并返回响应

func (*HTTPClient) SetCookieJar added in v1.1.4

func (c *HTTPClient) SetCookieJar(jar http.CookieJar)

SetCookieJar 设置自定义 Cookie Jar

func (*HTTPClient) SetInsecureSkipVerify added in v1.1.0

func (c *HTTPClient) SetInsecureSkipVerify(skip bool)

SetInsecureSkipVerify 设置跳过TLS证书验证

func (*HTTPClient) SetProxy added in v1.0.8

func (c *HTTPClient) SetProxy(proxyURL string) error

SetProxy 设置代理

func (*HTTPClient) SetTimeout added in v1.0.8

func (c *HTTPClient) SetTimeout(timeout time.Duration)

SetTimeout 设置超时时间

func (*HTTPClient) SetTransport added in v1.1.0

func (c *HTTPClient) SetTransport(transport *http.Transport)

SetTransport 设置自定义传输层

type HTTPRequest

type HTTPRequest struct {
	Method      string            // HTTP 方法 (GET, POST, PUT, DELETE 等)
	URL         string            // 请求 URL
	QueryParams map[string]string // URL 查询参数
	Headers     map[string]string // 请求头
	Body        interface{}       // 请求体 (支持多种类型)
	FormData    map[string]string // 表单数据 (字段名->值)
	FormFiles   map[string]string // 表单文件 (字段名->文件路径)
	Timeout     time.Duration     // 请求超时时间
	Context     context.Context   // 请求上下文
}

HTTPRequest 结构体封装 HTTP 请求参数

func NewHTTPRequest

func NewHTTPRequest(method, url string) *HTTPRequest

NewHTTPRequest 创建新的 HTTP 请求对象

func (*HTTPRequest) AddFormData added in v1.1.4

func (r *HTTPRequest) AddFormData(key, value string) *HTTPRequest

AddFormData 添加表单数据

func (*HTTPRequest) AddFormFile added in v1.1.4

func (r *HTTPRequest) AddFormFile(fieldName, filePath string) *HTTPRequest

AddFormFile 添加表单文件

func (*HTTPRequest) AddHeader added in v1.1.4

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

AddHeader 添加请求头

func (*HTTPRequest) AddQueryParam added in v1.1.4

func (r *HTTPRequest) AddQueryParam(key, value string) *HTTPRequest

AddQueryParam 添加查询参数

func (*HTTPRequest) BuildRequest added in v1.1.4

func (r *HTTPRequest) BuildRequest() (*http.Request, error)

BuildRequest 构建 HTTP 请求

func (*HTTPRequest) SetContext added in v1.1.4

func (r *HTTPRequest) SetContext(ctx context.Context) *HTTPRequest

SetContext 设置请求上下文

func (*HTTPRequest) SetFormData added in v1.1.4

func (r *HTTPRequest) SetFormData(formData map[string]string) *HTTPRequest

SetFormData 批量设置表单数据

func (*HTTPRequest) SetHeaders added in v1.1.4

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

SetHeaders 批量设置请求头

func (*HTTPRequest) SetJSONBody added in v1.1.4

func (r *HTTPRequest) SetJSONBody(body interface{}) *HTTPRequest

SetJSONBody 设置 JSON 请求体

func (*HTTPRequest) SetQueryParams added in v1.1.4

func (r *HTTPRequest) SetQueryParams(params map[string]string) *HTTPRequest

SetQueryParams 批量设置查询参数

func (*HTTPRequest) SetTimeout added in v1.1.4

func (r *HTTPRequest) SetTimeout(timeout time.Duration) *HTTPRequest

SetTimeout 设置请求超时时间

type HTTPResult added in v1.1.5

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

HTTPResult 封装 HTTP 响应

func (*HTTPResult) GetBodyString added in v1.1.5

func (r *HTTPResult) GetBodyString() (string, error)

GetBodyString 获取响应体字符串

func (*HTTPResult) ParseJSON added in v1.1.5

func (r *HTTPResult) ParseJSON(target interface{}) error

ParseJSON 解析 JSON 响应

func (*HTTPResult) ReadBodyBytes added in v1.1.8

func (r *HTTPResult) ReadBodyBytes() ([]byte, error)

func (*HTTPResult) Response added in v1.1.5

func (r *HTTPResult) Response() (*http.Response, error)

func (*HTTPResult) SaveToFile added in v1.1.5

func (r *HTTPResult) SaveToFile(path string) error

Jump to

Keyboard shortcuts

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