Documentation ¶
Overview ¶
包gclient提供了便捷的HTTP客户端功能。
Index ¶
- func X创建TLS配置(crt路径, key路径 string) (*tls.Config, error)
- type Client
- func (c *Client) Connect响应对象(上下文 context.Context, url string, 参数 ...interface{}) (*Response, error)
- func (c *Client) Connect字节集(上下文 context.Context, url string, 参数 ...interface{}) []byte
- func (c *Client) Connect文本(上下文 context.Context, url string, 参数 ...interface{}) string
- func (c *Client) Connect泛型类(上下文 context.Context, url string, 参数 ...interface{}) *泛型类.Var
- func (c *Client) Cookie(MapCookie map[string]string) *Client
- func (c *Client) Delete响应对象(上下文 context.Context, url string, 参数 ...interface{}) (*Response, error)
- func (c *Client) Delete字节集(上下文 context.Context, url string, 参数 ...interface{}) []byte
- func (c *Client) Delete文本(上下文 context.Context, url string, 参数 ...interface{}) string
- func (c *Client) Delete泛型类(上下文 context.Context, url string, 参数 ...interface{}) *泛型类.Var
- func (c *Client) Discovery(discovery gsvc.Discovery) *Client
- func (c *Client) DoRequestObj(ctx context.Context, req, res interface{}) error
- func (c *Client) Get响应对象(上下文 context.Context, url string, 参数 ...interface{}) (*Response, error)
- func (c *Client) Get字节集(上下文 context.Context, url string, 参数 ...interface{}) []byte
- func (c *Client) Get文本(上下文 context.Context, url string, 参数 ...interface{}) string
- func (c *Client) Get泛型类(上下文 context.Context, url string, 参数 ...interface{}) *泛型类.Var
- func (c *Client) Head响应对象(上下文 context.Context, url string, 参数 ...interface{}) (*Response, error)
- func (c *Client) Head字节集(上下文 context.Context, url string, 参数 ...interface{}) []byte
- func (c *Client) Head文本(上下文 context.Context, url string, 参数 ...interface{}) string
- func (c *Client) Head泛型类(上下文 context.Context, url string, 参数 ...interface{}) *泛型类.Var
- func (c *Client) Next(req *http.Request) (*Response, error)
- func (c *Client) Options响应对象(上下文 context.Context, url string, 参数 ...interface{}) (*Response, error)
- func (c *Client) Options字节集(上下文 context.Context, url string, 参数 ...interface{}) []byte
- func (c *Client) Options文本(上下文 context.Context, url string, 参数 ...interface{}) string
- func (c *Client) Options泛型类(上下文 context.Context, url string, 参数 ...interface{}) *泛型类.Var
- func (c *Client) Patch响应对象(上下文 context.Context, url string, 参数 ...interface{}) (*Response, error)
- func (c *Client) Patch字节集(上下文 context.Context, url string, 参数 ...interface{}) []byte
- func (c *Client) Patch文本(上下文 context.Context, url string, 参数 ...interface{}) string
- func (c *Client) Patch泛型类(上下文 context.Context, url string, 参数 ...interface{}) *泛型类.Var
- func (c *Client) Post响应对象(上下文 context.Context, url string, 参数 ...interface{}) (*Response, error)
- func (c *Client) Post字节集(上下文 context.Context, url string, 参数 ...interface{}) []byte
- func (c *Client) Post文本(上下文 context.Context, url string, 参数 ...interface{}) string
- func (c *Client) Post泛型类(上下文 context.Context, url string, 参数 ...interface{}) *泛型类.Var
- func (c *Client) Post表单响应对象(上下文 context.Context, url string, Map参数 map[string]string) (响应 *Response, 错误 error)
- func (c *Client) Put响应对象(上下文 context.Context, url string, 参数 ...interface{}) (*Response, error)
- func (c *Client) Put字节集(上下文 context.Context, url string, 参数 ...interface{}) []byte
- func (c *Client) Put文本(上下文 context.Context, url string, 参数 ...interface{}) string
- func (c *Client) Put泛型类(上下文 context.Context, url string, 参数 ...interface{}) *泛型类.Var
- func (c *Client) SetBuilder(builder gsel.Builder)
- func (c *Client) SetDiscovery(discovery gsvc.Discovery)
- func (c *Client) Trace响应对象(上下文 context.Context, url string, 参数 ...interface{}) (*Response, error)
- func (c *Client) Trace字节集(上下文 context.Context, url string, 参数 ...interface{}) []byte
- func (c *Client) Trace文本(上下文 context.Context, url string, 参数 ...interface{}) string
- func (c *Client) Trace泛型类(上下文 context.Context, url string, 参数 ...interface{}) *泛型类.Var
- func (c *Client) Url前缀(前缀 string) *Client
- func (c *Client) X中间件(中间件s ...X中间件函数) *Client
- func (c *Client) X代理(代理地址 string) *Client
- func (c *Client) X内容类型(contentType string) *Client
- func (c *Client) X内容类型json() *Client
- func (c *Client) X内容类型xml() *Client
- func (c *Client) X协议头(map协议头 map[string]string) *Client
- func (c *Client) X原始协议头(原始协议头 string) *Client
- func (c *Client) X取副本() *Client
- func (c *Client) X启用浏览器模式(启用 bool) *Client
- func (c *Client) X设置CookieMap(MapCookie map[string]string) *Client
- func (c *Client) X设置Map协议头(map协议头 map[string]string) *Client
- func (c *Client) X设置TLS配置(TLS配置 *tls.Config) error
- func (c *Client) X设置UA(UA值 string) *Client
- func (c *Client) X设置cookie(名称, 值 string) *Client
- func (c *Client) X设置url前缀(前缀 string) *Client
- func (c *Client) X设置代理(代理地址 string)
- func (c *Client) X设置内容类型(内容类型 string) *Client
- func (c *Client) X设置协议头(名称, 值 string) *Client
- func (c *Client) X设置原始协议头(原始协议头 string) *Client
- func (c *Client) X设置证书(crt路径, key路径 string) error
- func (c *Client) X设置请求参数禁止URL编码(禁止编码 bool) *Client
- func (c *Client) X设置账号密码(账号, 密码 string) *Client
- func (c *Client) X设置超时(时长 time.Duration) *Client
- func (c *Client) X设置重定向次数限制(次数 int) *Client
- func (c *Client) X设置重试与间隔(重试计数 int, 重试间隔时长 time.Duration) *Client
- func (c *Client) X请求参数禁止URL编码() *Client
- func (c *Client) X请求响应对象(上下文 context.Context, method, url string, 参数 ...interface{}) (响应 *Response, 错误 error)
- func (c *Client) X请求字节集(上下文 context.Context, 方法 string, url string, 参数 ...interface{}) []byte
- func (c *Client) X请求文本(上下文 context.Context, 方法 string, url string, 参数 ...interface{}) string
- func (c *Client) X请求泛型类(上下文 context.Context, 方法 string, url string, 参数 ...interface{}) *泛型类.Var
- func (c *Client) X账号密码(账号, 密码 string) *Client
- func (c *Client) X超时(时长 time.Duration) *Client
- func (c *Client) X重定向次数限制(次数 int) *Client
- func (c *Client) X重试与间隔(重试次数 int, 重试间隔时长 time.Duration) *Client
- type Response
- func (r *Response) X关闭() error
- func (r *Response) X取Cookie(名称 string) string
- func (r *Response) X取CookieMap() map[string]string
- func (r *Response) X取响应原始文本() string
- func (r *Response) X取响应字节集() []byte
- func (r *Response) X取响应文本() string
- func (r *Response) X取请求原始文本() string
- func (r *Response) X取请求和响应原始文本() string
- func (r *Response) X覆盖响应内容(字节集 []byte)
- func (r *Response) X请求和响应输出终端()
- type WebSocketClient
- type X中间件函数
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Client ¶
Client 是用于管理 HTTP 请求的 HTTP 客户端。
func (*Client) Connect响应对象 ¶
Connect 发送 CONNECT 请求并返回响应对象。 注意:如果响应对象将永不被使用,则必须关闭它。
func (*Client) Connect字节集 ¶
ConnectBytes 发送一个 CONNECT 请求,获取并以字节形式返回结果内容。
func (*Client) Connect泛型类 ¶
ConnectVar 发送一个 CONNECT 请求,获取并转换结果内容到指定的指针。 参数 `pointer` 的类型可以是:struct/*struct/**struct/[]struct/[]*struct/*[]struct 等。
func (*Client) Cookie ¶
Cookie 是一个链式函数, 用于为下一次请求通过映射设置cookie项。
Example ¶
var ( url = "http://127.0.0.1:8999/cookie" cookie = g.MapStrStr{ "SessionId": "123", } ) content := g.X网页类().Cookie(cookie).Post文本(ctx, url, g.Map{ "id": 10000, "name": "john", }) fmt.Println(content)
Output: SessionId: 123
func (*Client) Delete响应对象 ¶
Delete 发送DELETE请求并返回响应对象。 注意:如果响应对象将永远不会被使用,那么它必须被关闭。
func (*Client) Delete泛型类 ¶
DeleteVar 发送一个 DELETE 请求,获取并转换结果内容为指定的指针类型。 参数 `pointer` 可以是以下类型:struct/*struct/**struct/[]struct/[]*struct/*[]struct 等等。
func (*Client) DoRequestObj ¶
DoRequestObj 使用标准请求/响应对象执行HTTP请求。 请求对象`req`的定义类似如下:
type UseCreateReq struct { g.Meta `path:"/user" method:"put"`
// 其他字段...
}
响应对象`res`应该是指针类型。如果成功,它会自动将结果转换为给定的对象`res`。 例如:
var ( req = UseCreateReq{} res *UseCreateRes )
DoRequestObj(ctx, req, &res) 这段代码注释翻译成中文的大致意思是:该函数DoRequestObj利用标准的请求/响应对象进行HTTP请求操作。其中,请求对象`req`是一个结构体,其定义中包含了用于指定HTTP路径和方法的元数据字段。响应对象`res`应当是指针类型,当HTTP请求成功时,函数会自动将响应结果转换并填充到这个`res`对象中。示例代码展示了如何初始化请求与响应对象,并调用DoRequestObj函数执行请求。
func (*Client) Get泛型类 ¶
GetVar 发送一个GET请求,获取并转换结果内容为指定的指针类型。 参数`pointer`可以是以下类型:struct/*struct/**struct/[]struct/[]*struct/*[]struct等。
func (*Client) Head泛型类 ¶
HeadVar 发送一个 HEAD 请求,获取并转换结果内容到指定的指针。 参数 `pointer` 可以是以下类型:struct/*struct/**struct/[]struct/[]*struct/*[]struct 等等。
func (*Client) Options响应对象 ¶
Options 发送 OPTIONS 请求并返回响应对象。 注意,如果响应对象将永远不会被使用,则必须关闭它。
func (*Client) Options字节集 ¶
OptionsBytes 发送一个 OPTIONS 请求,获取并以字节形式返回结果内容。
func (*Client) Options泛型类 ¶
OptionsVar 发送一个 OPTIONS 请求,获取并转换结果内容到指定的指针。 参数 `pointer` 的类型可以是:struct/*struct/[]struct/[]*struct/*[]struct 等。
func (*Client) Patch泛型类 ¶
PatchVar 发送一个 PATCH 请求,获取并转换结果内容为指定的指针类型。 参数 `pointer` 的类型可以是:struct/*struct/**struct/[]struct/[]*struct/*[]struct 等。
func (*Client) Post泛型类 ¶
PostVar 发送一个POST请求,获取并转换结果内容为指定的指针类型。 参数`pointer`可以是以下类型:struct/*struct/**struct/[]struct/[]*struct/*[]struct等。
func (*Client) Post表单响应对象 ¶
func (c *Client) Post表单响应对象(上下文 context.Context, url string, Map参数 map[string]string) (响应 *Response, 错误 error)
PostForm 与 net/http.PostForm 不同。 它是 Post 方法的一个包装器,会将 Content-Type 设置为 "multipart/form-data;"。 并且它会自动为请求体和 Content-Type 设置边界字符。
其效果类似于以下情况:
Content-Type: multipart/form-data; boundary=----Boundarye4Ghaog6giyQ9ncN
表单数据格式如下: ------Boundarye4Ghaog6giyQ9ncN Content-Disposition: form-data; name="checkType"
none
该方法用于发送表单数据。 注意,如果响应对象不再使用,则必须关闭它。
func (*Client) Put泛型类 ¶
PutVar 发送一个 PUT 请求,获取并转换结果内容为指定的指针类型。 参数 `pointer` 可以是以下类型:struct、*struct、**struct、[]struct、[]*struct、*[]struct 等等。
func (*Client) SetBuilder ¶
SetBuilder 为客户端设置负载均衡器生成器。
func (*Client) SetDiscovery ¶
SetDiscovery 设置客户端的负载均衡器生成器。
func (*Client) Trace泛型类 ¶
TraceVar 发送一个 TRACE 请求,获取并转换结果内容到指定的指针。 参数 `pointer` 可以是以下类型:struct/*struct/**struct/[]struct/[]*struct/*[]struct 等等。
func (*Client) Url前缀 ¶
Prefix 是一个链式函数, 它用于设置该客户端下一次请求的URL前缀。 示例: Prefix("http://127.0.0.1:8199/api/v1") // 设置URL前缀为v1版本 Prefix("http://127.0.0.1:8199/api/v2") // 设置URL前缀为v2版本
func (*Client) X代理 ¶
Proxy 是一个链式函数, 用于设置下一次请求的代理。 请确保传递正确的 `proxyURL`。 正确的格式应为 `http://USER:PASSWORD@IP:PORT` 或 `socks5://USER:PASSWORD@IP:PORT`。 目前仅支持 `http` 和 `socks5` 类型的代理。
func (*Client) X内容类型json ¶
ContentJson 是一个链式函数, 它用于为下一个请求设置HTTP内容类型为 "application/json"。
注意,它还会自动检查并以JSON格式对参数进行编码。
func (*Client) X内容类型xml ¶
ContentXml 是一个链式函数, 它为下一次请求设置HTTP内容类型为"application/xml"。
注意,它还会自动检查并以XML格式对参数进行编码。
func (*Client) X设置CookieMap ¶
SetCookieMap 通过map设置cookie项目。
func (*Client) X设置代理 ¶
SetProxy 为客户端设置代理。 当参数`proxyURL`为空或者格式不正确时,此函数将不做任何操作。 正确的格式应如 `http://USER:PASSWORD@IP:PORT` 或 `socks5://USER:PASSWORD@IP:PORT`。 目前仅支持 `http` 和 `socks5` 类型的代理。
func (*Client) X设置请求参数禁止URL编码 ¶
SetNoUrlEncode 设置标记,表示在发送请求前不对参数进行URL编码。
func (*Client) X请求参数禁止URL编码 ¶
NoUrlEncode 设置标记,表示在发送请求前不应对参数进行URL编码。
func (*Client) X请求响应对象 ¶
func (c *Client) X请求响应对象(上下文 context.Context, method, url string, 参数 ...interface{}) (响应 *Response, 错误 error)
DoRequest 使用给定的HTTP方法和数据发送请求,并返回响应对象。 注意,如果响应对象将不再使用,则必须关闭它。
注意,如果请求包含文件上传,则它使用"multipart/form-data"作为其Content-Type, 否则使用"application/x-www-form-urlencoded"。它还会自动检测POST内容的JSON格式, 对于JSON格式的内容,会自动将其Content-Type设置为"application/json"。
func (*Client) X请求泛型类 ¶
RequestVar 使用给定的HTTP方法和数据发送请求,检索并将结果转换为指定的指针。 它会自动内部读取并关闭响应对象。 参数`pointer`可以是以下类型:struct/*struct/**struct/[]struct/[]*struct/*[]struct等。
type Response ¶
type Response struct { *http.Response // Response 是某个请求的底层 http.Response 对象。 // contains filtered or unexported fields }
Response 是用于客户端请求响应的结构体。
func (*Response) X取CookieMap ¶
GetCookieMap 获取并返回当前cookie值的映射副本。
type WebSocketClient ¶
WebSocketClient 对底层的websocket客户端连接进行封装, 并提供了便捷的功能方法。