httpclient

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HttpClient

type HttpClient struct {
	Client    *http.Client
	Timeout   time.Duration
	UserAgent string
	// contains filtered or unexported fields
}

HttpClient http客户端

func New

func New(opts ...Option) *HttpClient

New 创建一个http客户端

func (*HttpClient) Delete

func (c *HttpClient) Delete(ctx context.Context, url string, opts ...RequestOption) (*Response, error)

Delete 发送DELETE请求

func (*HttpClient) Do

func (c *HttpClient) Do(ctx context.Context, method Method, url string, body io.Reader, opts ...RequestOption) (*Response, error)

Do 发送请求

func (*HttpClient) Get

func (c *HttpClient) Get(ctx context.Context, url string, opts ...RequestOption) (*Response, error)

Get 发送GET请求

func (*HttpClient) Post

func (c *HttpClient) Post(ctx context.Context, url string, body io.Reader, opts ...RequestOption) (*Response, error)

Post 发送POST请求

func (*HttpClient) PostFile

func (c *HttpClient) PostFile(ctx context.Context, url string, fileName, filePath string, extraFields map[string]string, opts ...RequestOption) (*Response, error)

PostFile 发送POST请求,请求体为文件

func (*HttpClient) PostForm

func (c *HttpClient) PostForm(ctx context.Context, url string, data url.Values, opts ...RequestOption) (*Response, error)

PostForm 发送POST请求,请求体为表单

func (*HttpClient) PostJSON

func (c *HttpClient) PostJSON(ctx context.Context, url string, data any, opts ...RequestOption) (*Response, error)

PostJSON 发送POST请求,请求体为JSON

func (*HttpClient) Put

func (c *HttpClient) Put(ctx context.Context, url string, body io.Reader, opts ...RequestOption) (*Response, error)

Put 发送PUT请求

type Method

type Method string

Method 请求方法类型定义

const (
	GET    Method = "GET"
	POST   Method = "POST"
	PUT    Method = "PUT"
	DELETE Method = "DELETE"
	PATCH  Method = "PATCH"
)

type Option

type Option func(*HttpClient)

Option 配置项

func WithLogger

func WithLogger(l logger.Logger) Option

WithLogger 设置日志实例

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout 设置超时时间

func WithUserAgent

func WithUserAgent(userAgent string) Option

WithUserAgent 设置User-Agent

type RequestOption

type RequestOption func(*http.Request)

RequestOption 请求配置项

func WithCookies

func WithCookies(cookies []*http.Cookie) RequestOption

WithCookies 添加cookies

func WithHeader

func WithHeader(key, value string) RequestOption

WithHeader 设置请求头

func WithQuery

func WithQuery(query url.Values) RequestOption

WithQuery 设置请求参数

type Response

type Response struct {
	StatusCode int
	Header     http.Header
	Body       []byte
	Request    *http.Request
	Cookies    []*http.Cookie
}

Response 响应结构体

func (*Response) GetCookie

func (r *Response) GetCookie(name string) *http.Cookie

GetCookie 获取指定名称的cookie

func (*Response) IsSuccess

func (r *Response) IsSuccess() bool

IsSuccess 判断响应是否成功

func (*Response) JSON

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

JSON 解析响应为JSON

func (*Response) String

func (r *Response) String() string

String 解析响应为字符串

Jump to

Keyboard shortcuts

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