http_utils

package
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 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) (*HTTPResponse, error)

Do 执行HTTP请求

func (*HTTPClient) DownloadFile added in v1.0.8

func (c *HTTPClient) DownloadFile(url, filePath string) error

DownloadFile 下载文件到指定路径

func (*HTTPClient) EnableConnectionPool added in v1.1.4

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

EnableConnectionPool 启用连接池

func (*HTTPClient) Execute added in v1.1.4

func (c *HTTPClient) Execute(request *HTTPRequest) (*HTTPResponse, error)

Execute 执行HTTP请求并返回响应

func (*HTTPClient) Get added in v1.1.4

func (c *HTTPClient) Get(url string, headers map[string]string) (*HTTPResponse, error)

Get 执行GET请求

func (*HTTPClient) Post added in v1.1.4

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

func (*HTTPClient) PostForm added in v1.1.4

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

PostForm 执行表单POST请求

func (*HTTPClient) PostJSON added in v1.1.4

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

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) (*HTTPResponse, error)

PostMultipart 执行multipart/form-data POST请求

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 HTTPResponse added in v1.1.4

type HTTPResponse struct {
	StatusCode int               // 状态码
	Headers    map[string]string // 响应头
	Body       []byte            // 响应体
	RequestURL string            // 请求URL
	Duration   time.Duration     // 请求耗时
}

HTTPResponse 封装 HTTP 响应

func (*HTTPResponse) GetBodyString added in v1.1.4

func (resp *HTTPResponse) GetBodyString() string

GetBodyString 获取响应体字符串

func (*HTTPResponse) IsSuccess added in v1.1.4

func (resp *HTTPResponse) IsSuccess() bool

IsSuccess 判断请求是否成功 (2xx 状态码)

func (*HTTPResponse) ParseJSON added in v1.1.4

func (resp *HTTPResponse) ParseJSON(target interface{}) error

ParseJSON 解析 JSON 响应

Jump to

Keyboard shortcuts

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