Documentation ¶
Index ¶
- Constants
- func ConvertGBKToUTF8(gbkData []byte) ([]byte, error)
- func IsStringEmpty(str string) bool
- type Client
- func (c *Client) ClearCookies() *Client
- func (c *Client) Get(url string, params, headers map[string]string) (*Response, error)
- func (c *Client) GetCookies(url string) ([]*http.Cookie, error)
- func (c *Client) Head(url string, params, headers map[string]string) (*Response, error)
- func (c *Client) Post(url string, params, headers map[string]string) (*Response, error)
- func (c *Client) PostForm(url string, data, headers map[string]string) (*Response, error)
- func (c *Client) PostJSON(url string, data any, headers map[string]string) (*Response, error)
- func (c *Client) R() *Request
- func (c *Client) SetAuthScheme(scheme string) *Client
- func (c *Client) SetBaseURL(baseURL string) *Client
- func (c *Client) SetBasicAuth(username, password string) *Client
- func (c *Client) SetBasicAuthToken(token string) *Client
- func (c *Client) SetBody(body string) *Client
- func (c *Client) SetBodyJSON(data interface{}) *Client
- func (c *Client) SetCheckRedirect(f func(req *http.Request, via []*http.Request) error) *Client
- func (c *Client) SetCookie(cookies string) *Client
- func (c *Client) SetCookieRaw(cookie *http.Cookie) *Client
- func (c *Client) SetCookiesRaw(cookies []*http.Cookie) *Client
- func (c *Client) SetDebug(debug bool) *Client
- func (c *Client) SetFormParam(key, value string) *Client
- func (c *Client) SetFormParams(params map[string]string) *Client
- func (c *Client) SetHandleRequestResultFunc(f HandleRequestResult) *Client
- func (c *Client) SetHeader(key, value string) *Client
- func (c *Client) SetHeaders(headers map[string]string) *Client
- func (c *Client) SetMethod(method string) *Client
- func (c *Client) SetProxyURL(proxy string) *Client
- func (c *Client) SetQueryParam(key, value string) *Client
- func (c *Client) SetQueryParams(params map[string]string) *Client
- func (c *Client) SetRetryMax(retryMax int) *Client
- func (c *Client) SetTimeout(timeout time.Duration) *Client
- func (c *Client) SetUserAgent(userAgent string) *Client
- type HandleRequestResult
- type HandleResponseResult
- type LeveledLogger
- type Request
- func (r *Request) AddHeader(key, value string) *Request
- func (r *Request) DelHeader(key string) *Request
- func (r *Request) DelQueryParam(key string) *Request
- func (r *Request) Do() (*Response, error)
- func (r *Request) Execute(urlPath string) (*Response, error)
- func (r *Request) GetHeader(key string) string
- func (r *Request) SetBody(body string) *Request
- func (r *Request) SetBodyBytes(body []byte) *Request
- func (r *Request) SetBodyJSON(data any) *Request
- func (r *Request) SetContext(ctx context.Context) *Request
- func (r *Request) SetCookie(cookies string) *Request
- func (r *Request) SetCookieRaw(cookie *http.Cookie) *Request
- func (r *Request) SetCookiesRaw(cookies []*http.Cookie) *Request
- func (r *Request) SetFormParam(key, value string) *Request
- func (r *Request) SetFormParams(params map[string]string) *Request
- func (r *Request) SetHeader(key, value string) *Request
- func (r *Request) SetHeaders(headers map[string]string) *Request
- func (r *Request) SetMethod(method string) *Request
- func (r *Request) SetQueryParam(key, value string) *Request
- func (r *Request) SetQueryParams(params map[string]string) *Request
- type Response
- func Get(url string, params, headers map[string]string) (*Response, error)
- func Head(url string, params, headers map[string]string) (*Response, error)
- func PostForm(url string, data, headers map[string]string) (*Response, error)
- func PostJSON(url string, data any, headers map[string]string) (*Response, error)
- func (r *Response) Body() []byte
- func (r *Response) DetectEncoding() error
- func (r *Response) GetCookies() []*http.Cookie
- func (r *Response) GetHeader(key string) string
- func (r *Response) GetHeaderValues(key string) []string
- func (r *Response) Gjson() gjson.Result
- func (r *Response) HasHeader(key string) bool
- func (r *Response) Header() http.Header
- func (r *Response) IsClientError() bool
- func (r *Response) IsServerError() bool
- func (r *Response) IsSuccess() bool
- func (r *Response) JSON(v interface{}) error
- func (r *Response) PrettyPrint() string
- func (r *Response) SaveToFile(filepath string) error
- func (r *Response) StatusCode() int
- func (r *Response) String() string
- func (r *Response) ToBytesBuffer() *bytes.Buffer
- func (r *Response) ToMap() (map[string]interface{}, error)
- type User
Constants ¶
const ( ContentTypeJson = "application/json" ContentTypeForm = "application/x-www-form-urlencoded" ContentTypeXml = "application/xml" ContentTypeStream = "application/octet-stream" ContentTypeText = "text/plain" ContentTypeHtml = "text/html" ContentTypeMultipart = "multipart/form-data" )
Variables ¶
This section is empty.
Functions ¶
func ConvertGBKToUTF8 ¶
ConvertGBKToUTF8 将 GBK 编码的字节数组转换为 UTF-8 编码
func IsStringEmpty ¶
IsStringEmpty method tells whether given string is empty or not
Types ¶
type Client ¶
type Client struct { *http.Client // HTTP 客户端 BasicAuthToken string // 基本认证令牌 HeaderAuthorizationKey string // 认证头部键 AuthScheme string // 认证方案 Method string // 请求方法 BaseURL string // 请求的基础 URL Timeout time.Duration // 请求超时 Logger LeveledLogger // 日志记录器 RetryMax int // 最大重试次数 Cookies []*http.Cookie // 每个请求都要发送的 cookie Header http.Header // 每个请求都要发送的头部 QueryParams map[string]string // 请求的查询参数 Body string // 请求的主体内容 FormParams urlpkg.Values // 表单参数 Debug bool // 是否启用调试模式 UserInfo *User // 用户信息, 用于请求认证 // contains filtered or unexported fields }
Client 用于发出 HTTP 请求,添加了额外的功能
func (*Client) ClearCookies ¶
func (*Client) GetCookies ¶
GetCookies get cookies from the underlying `http.Client`'s `CookieJar`.
func (*Client) PostForm ¶
PostForm is a convenience method for doing simple POST operations using pre-filled url.Values form data.
func (*Client) PostJSON ¶
PostJSON is a convenience method for doing simple POST operations using JSON data.
func (*Client) SetAuthScheme ¶
func (*Client) SetBasicAuth ¶
func (*Client) SetBasicAuthToken ¶
func (*Client) SetBodyJSON ¶
SetBodyJSON 将请求体设置为 JSON 对象
func (*Client) SetCheckRedirect ¶
SetCheckRedirect 设置重定向函数
func (*Client) SetCookieRaw ¶
SetCookieRaw 增加单个原始 cookie
func (*Client) SetCookiesRaw ¶
SetCookiesRaw 设置原始 cookie 切片
func (*Client) SetFormParam ¶
SetFormParam 设置单个表单参数
func (*Client) SetFormParams ¶
SetFormParams 设置多个表单参数
func (*Client) SetHandleRequestResultFunc ¶
func (c *Client) SetHandleRequestResultFunc(f HandleRequestResult) *Client
func (*Client) SetHeaders ¶
SetHeaders 设置多个请求头部
func (*Client) SetProxyURL ¶
SetProxyURL 设置代理服务器 URL
func (*Client) SetQueryParam ¶
SetQueryParam 设置单个查询参数
func (*Client) SetQueryParams ¶
SetQueryParams 设置多个查询参数
func (*Client) SetRetryMax ¶
SetRetryMax 设置最大重试次数
func (*Client) SetTimeout ¶
SetTimeout 设置请求超时
func (*Client) SetUserAgent ¶
SetUserAgent 设置 User-Agent 头
type HandleResponseResult ¶
type LeveledLogger ¶
type LeveledLogger interface { Error(msg string, keysAndValues ...interface{}) Info(msg string, keysAndValues ...interface{}) Debug(msg string, keysAndValues ...interface{}) Warn(msg string, keysAndValues ...interface{}) WithContext(ctx context.Context) LeveledLogger }
LeveledLogger 接口定义了分级日志记录的方法
type Request ¶
type Request struct { *http.Request GetBody func() (io.ReadCloser, error) Header http.Header // contains filtered or unexported fields }
Request 封装了 HTTP 请求及其相关数据
func (*Request) DelQueryParam ¶
DelQueryParam 删除查询参数
func (*Request) SetBodyBytes ¶
SetBodyBytes 设置请求体为字节数组
func (*Request) SetBodyJSON ¶
func (*Request) SetCookieRaw ¶
SetCookieRaw 设置单个原始 Cookie
func (*Request) SetCookiesRaw ¶
SetCookiesRaw 设置原始 Cookie 切片
func (*Request) SetFormParam ¶
SetFormParam 设置单个表单参数
func (*Request) SetFormParams ¶
SetFormParams 设置多个表单参数
func (*Request) SetHeaders ¶
SetHeaders 设置多个请求头
func (*Request) SetQueryParam ¶
SetQueryParam 设置单个查询参数
type Response ¶
Response 封装了 HTTP 响应,提供了便捷的方法来处理响应。
func PostForm ¶
PostForm is a shortcut to perform a POST with form data without creating a new client.
func PostJSON ¶
PostJSON is a shortcut to perform a POST with JSON data without creating a new client.
func (*Response) DetectEncoding ¶
DetectEncoding 检测响应体的编码并转换为 UTF-8
func (*Response) GetCookies ¶
GetCookies 获取响应的 Cookies
func (*Response) GetHeaderValues ¶
GetHeaderValues 获取指定的响应头的所有值
func (*Response) IsClientError ¶
IsClientError 检查响应是否表示客户端错误。
func (*Response) IsServerError ¶
IsServerError 检查响应是否表示服务器错误。
func (*Response) SaveToFile ¶
SaveToFile 将响应体保存到指定文件。
func (*Response) ToBytesBuffer ¶
ToBytesBuffer 返回响应体的字节缓冲区。