Documentation ¶
Index ¶
- Constants
- Variables
- type Client
- func BaseURL(url string) *Client
- func Cookie(k string, v string) *Client
- func Header(key, val string) *Client
- func New() *Client
- func Proxy(url string) *Client
- func Query(query interface{}) *Client
- func Retry(n int) *Client
- func Timeout(timeout time.Duration) *Client
- func Type(name string) *Client
- func Use(mdl ClientMiddleware) *Client
- func UseResponse(mdl responseMiddlewareHandler) *Client
- func UserAgent(name string) *Client
- func (c *Client) BaseURL(url string) *Client
- func (c *Client) Clone() *Client
- func (c *Client) Config(conf *ClientConfig) *Client
- func (c *Client) Cookie(k string, v string) *Client
- func (c *Client) Del(url string, args ...interface{}) (*Response, error)
- func (c *Client) DoRequest(method, u string, data ...interface{}) (resp *Response, err error)
- func (c *Client) Get(url string, args ...interface{}) (*Response, error)
- func (c *Client) Head(url string, data ...interface{}) (*Response, error)
- func (c *Client) Header(k string, v interface{}) *Client
- func (c *Client) Options(url string, args ...interface{}) (*Response, error)
- func (c *Client) Patch(url string, args ...interface{}) (*Response, error)
- func (c *Client) Post(url string, args ...interface{}) (*Response, error)
- func (c *Client) Proxy(url string) *Client
- func (c *Client) Put(url string, args ...interface{}) (*Response, error)
- func (c *Client) Query(query interface{}) *Client
- func (c *Client) Retry(n int) *Client
- func (c *Client) Safe(s ...bool) *Client
- func (c *Client) SetClient(rawClient http.Client) *Client
- func (c *Client) Timeout(timeout time.Duration) *Client
- func (c *Client) TransformRequest(h requestMiddlewareHandler) *Client
- func (c *Client) TransformResponse(h responseMiddlewareHandler) *Client
- func (c *Client) Type(ty string) *Client
- func (c *Client) Use(mdl ClientMiddleware) *Client
- func (c *Client) UserAgent(name string) *Client
- type ClientConfig
- type ClientMiddleware
- type HttpLogger
- type Response
- func Del(url string, data ...interface{}) (*Response, error)
- func Get(url string, data ...interface{}) (*Response, error)
- func Head(url string, data ...interface{}) (*Response, error)
- func Options(url string, data ...interface{}) (*Response, error)
- func Patch(url string, data ...interface{}) (*Response, error)
- func Post(url string, data ...interface{}) (*Response, error)
- func Put(url string, data ...interface{}) (*Response, error)
- func (rp *Response) AddError(err error)
- func (rp *Response) Body() (bt []byte)
- func (rp *Response) Byte() (bt []byte)
- func (rp *Response) Cookie() []*http.Cookie
- func (rp *Response) Error() string
- func (rp *Response) GetError() error
- func (rp *Response) Header() http.Header
- func (rp *Response) JSON(v interface{}) error
- func (rp *Response) ReadAllBody() (bt []byte, err error)
- func (rp *Response) SetBody(bt []byte)
- func (rp *Response) Status() int
- func (rp *Response) StatusCode() int
- func (rp *Response) String() string
- func (rp *Response) XML(v interface{}) error
- type ResponseError
Constants ¶
View Source
const ( TypeJSON = "json" TypeXML = "xml" TypeUrlencoded = "urlencoded" TypeForm = "form" TypeFormData = "form-data" TypeHTML = "html" TypeText = "text" TypeMultipart = "multipart" )
Types we support.
Variables ¶
View Source
var Logger = AccessLogger(consoleLogger{})
View Source
var Types = map[string]string{ TypeJSON: "application/json", TypeXML: "application/xml", TypeForm: "application/x-www-form-urlencoded", TypeFormData: "application/x-www-form-urlencoded", TypeUrlencoded: "application/x-www-form-urlencoded", TypeHTML: "text/html", TypeText: "text/plain", TypeMultipart: "multipart/form-data", }
Functions ¶
This section is empty.
Types ¶
type Client ¶
func (*Client) Config ¶ added in v1.4.4
func (c *Client) Config(conf *ClientConfig) *Client
Config 批量设置 HTTP Client 的配置项
func (*Client) DoRequest ¶
DoRequest 发起请求, data 参数在 GET, HEAD 请求时被解析为查询参数,在其他方法请求时根据 Content-Type 解析为其他数据类型,如 json, url-form-encoding, xml 等等
func (*Client) TransformRequest ¶
TransformRequest 增加请求中间件,可以在请求发起前对整个请求做前置处理,比如修改 body, header, proxy, url, 配置项等等,也可以获取请求的各种数据,如自定义日志,类似于 axios 的 transformRequest
func (*Client) TransformResponse ¶
TransformResponse 增加响应中间件,可以在收到请求后第一时间对请求做处理,如验证 status code,验证 body 数据,甚至重置 body 数据,更改响应等等任何操作,类似于 axios 的 transformResponse
func (*Client) Use ¶
func (c *Client) Use(mdl ClientMiddleware) *Client
Use 应用中间件,实现了 TransformRequest 和 TransformResponse 接口的中间件,如 http.Use(http.AccessLogger()),通常用于成对的请求响应处理
type ClientConfig ¶ added in v1.4.4
type ClientConfig struct { TransformRequest requestMiddlewareHandler // 请求中间件 TransformResponse responseMiddlewareHandler // 响应中间件 Headers map[string]string // 预设 Header Cookies map[string]string // 预设请求 Cookie Type string // 预设请求数据类型, 该配置为空时忽略 UserAgent string // 预设 User-Agent, 该配置为空时忽略 Proxy string // 预设请求代理,该配置为空时忽略 BaseURL string // 预设 url 前缀,叠加 Query interface{} // 预设请求查询参数 Timeout time.Duration // 请求超时时间,该配置为 0 时忽略 Retry int // 重试次数,该配置为 0 时忽略 }
批量设置 HTTP Client 的配置项
type ClientMiddleware ¶
type HttpLogger ¶
type HttpLogger struct { Logger printLogger MaxLoggerBodyLen int64 }
打印 HTTP 请求响应日志
func AccessLogger ¶
func AccessLogger(logger printLogger, bodyLimit ...int64) *HttpLogger
func (*HttpLogger) TransformRequest ¶
func (l *HttpLogger) TransformRequest(c *Client, req *http.Request) *Client
打印 HTTP 请求日志
func (*HttpLogger) TransformResponse ¶
打印 HTTP 响应日志 warning: 会先读取一遍 Response.Body,如果该中间件导致了 http 下载异常问题,请关闭该中间件
type Response ¶
type Response struct { Client *Client Request *http.Request Response *http.Response Err *ResponseError IsRead bool // contains filtered or unexported fields }
func (*Response) ReadAllBody ¶
ReadAllBody 读取响应的 Body 流
type ResponseError ¶
type ResponseError struct {
// contains filtered or unexported fields
}
func (*ResponseError) Add ¶
func (e *ResponseError) Add(err error)
func (*ResponseError) Error ¶
func (e *ResponseError) Error() string
Click to show internal directories.
Click to hide internal directories.