resty

package
v0.0.0-...-7113a4a Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

非200响应码会正常返回响应,不会返回错误

Index

Constants

This section is empty.

Variables

View Source
var Client *resty.Client

Client http客户端 SetDebug() Client logs details of every request and response. SetDebugBodyLimit() log请求/响应体最大长度

RemoveProxy() 移除代理配置 IsProxySet() 客户端是否设置了代理 默认会使用环境变量的代理,参考 http.ProxyFromEnvironment

R() 创建请求实例 SetRetryCount() 启用重试并设置次数 SetTimeout() 设置超时时间

View Source
var (
	// New method creates a new Resty client.
	New = resty.New
)

Functions

func Execute

func Execute(method, url string, hook ReqHook) (*resty.Response, error)

Execute 使用指定方法请求 http.MethodGet

func Get

func Get(url string, hook ReqHook) (*resty.Response, error)

func Init

func Init()

Init 初始化客户端

func Post

func Post(url string, hook ReqHook) (*resty.Response, error)

func PostFile

func PostFile(url string, hook ReqHook, name string) (*resty.Response, error)

PostFile 上传文件,直接读取文件字节 Content-Type 根据内容检测

func PostLog

func PostLog(req *resty.Request, url string, info map[string]any) (*resty.Response, error)

PostLog post请求并记录日志 传入请求对象 请求前 路径 请求体 自定义日志 请求后 路径 请求体 自定义日志 响应码+描述 响应体 耗时

func PostNew

func PostNew(
	headers map[string]string,
	body, result any,
	url string,
	info map[string]any,
) (*resty.Response, error)

PostNew post请求并记录日志 新创建客户端

func SetProxy

func SetProxy(proxyURL string)

SetProxy 配置客户端代理 http://user:password@proxyserver:8888 默认会使用环境变量的代理,参考 http.ProxyFromEnvironment 会自动初始化客户端 支持socks5

func SetProxyFive

func SetProxyFive(scheme, host, port, user, pass string)

SetProxyFive 配置客户端代理 scheme, host, port, user, password 默认会使用环境变量的代理,参考 http.ProxyFromEnvironment 会自动初始化客户端 支持socks5

func SetProxyPart

func SetProxyPart(uri, user, pass string) error

SetProxyPart 配置客户端代理 http://proxyserver:8888, user, password 默认会使用环境变量的代理,参考 http.ProxyFromEnvironment 会自动初始化客户端 支持socks5

func SetProxySct

func SetProxySct(u *url.URL)

SetProxySct 配置客户端代理,使用url.URL结构体 默认会使用环境变量的代理,参考 http.ProxyFromEnvironment 会自动初始化客户端 支持socks5

func ToError

func ToError(r *resty.Response) error

ToError 请求失败时,返回错误

Types

type ReqHook

type ReqHook = func(*resty.Request)

ReqHook 设置请求信息 查询字符串,键区分大小写 SetQueryParam 单个参数,同一个键会覆盖 SetQueryParams 多个参数 SetQueryParamsFromValues 多个参数,参数可能有多个值 SetQueryString 直接设置字符串

请求头 键不区分大小写,规范格式 Content-Type 多个值格式为 "text/html, application/xml;q=0.9" SetHeader 单个参数,同一个键会覆盖 SetHeaderMultiValues 多个参数,参数可能有多个值 SetHeaders 多个参数,同一个键会覆盖

cookie SetCookie 单个cookie SetCookies 多个cookie

请求体 SetBody 支持类型`string`, `[]byte`, `struct`, `map`, `slice`, `io.Reader`,支持指针 struct、map、slice类型自动json编码,并设置 Content-Type 为 application/json string类型 Content-Type 为 "text/plain; charset=utf-8" []byte类型 Content-Type 根据内容检测,保底为 "text/plain; charset=utf-8"

表单 只传键值 Content-Type 为 application/x-www-form-urlencoded 上传文件 Content-Type 为 multipart/form-data SetFile 上传单文件 SetFileReader 上传单文件,使用io.Reader SetFiles 上传多文件 SetFormData 设置表单键值对 SetFormDataFromValues 设置表单键值对,支持多值

认证请求头 `Authorization: <scheme> <token>` https://www.iana.org/assignments/http-authschemes/http-authschemes.xhtml SetAuthScheme 认证方案,默认为Bearer SetAuthToken 认证令牌 SetBasicAuth basic认证 "Basic <base64-encoded-value>"

响应体自动解码,当类型为json或xml时 SetResult 指定响应体结构,响应码200~299 当指定结构非指针时,与Response.Result配合使用 当指定结构为指针时,与Response.Result配合使用,或直接使用传入的指针 SetError 指定响应体结构,响应码>399 当指定结构非指针时,与Response.Error配合使用 当指定结构为指针时,与Response.Error配合使用,或直接使用传入的指针

EnableTrace 是否追踪请求信息 SetOutput 指定下载路径

type Request

type Request = resty.Request

请求 Body 请求体

Post() post方法

type Res

type Res struct {
	RestyRequest  *resty.Request
	RestyResponse *resty.Response
	HttpResponse  *http.Response

	Url        string         // 请求链接
	StatusCode int            // 状态码
	Status     string         // 状态字符串
	Proto      string         // 协议版本
	IsSuccess  bool           // 状态码是200~299
	IsError    bool           // 状态码大于等于400
	Header     http.Header    // 响应头
	Cookies    []*http.Cookie // 响应cookie
	Size       int64          // 响应体字节数
	Body       []byte         // 响应体字节
	String     string         // 响应体字符串,去掉首尾空白
	// 响应体自动解码,类型断言用指针
	Result any // 响应码200~299,与Request.SetResult配合使用
	Error  any // 响应码>399,与Request.SetError配合使用

	IsJson bool // 响应体是否为json编码

	// 统计信息
	// TotalTime ≈ ConnTime + ServerTime + ResponseTime
	// ConnTime ≈ DNSLookup + TCPConnTime + TLSHandshake
	TotalTime  time.Duration // 请求响应总时间
	SentAt     time.Time     // 请求发送时间
	ReceivedAt time.Time     // 响应接收时间

	// 请求追踪信息,EnableTrace后才会有值
	resty.TraceInfo
	RemoteAddr     string        // 目标网络地址 ip:host
	DNSLookup      time.Duration // dns查找时间
	ConnTime       time.Duration // 总连接时间
	TCPConnTime    time.Duration // tcp连接时间
	TLSHandshake   time.Duration // tls握手时间
	ServerTime     time.Duration // 服务处理耗时
	ResponseTime   time.Duration // 响应持续耗时
	IsConnReused   bool          // tcp连接是否之前被使用过
	IsConnWasIdle  bool          // tcp连接是否从空闲池获取
	ConnIdleTime   time.Duration // 如果tcp连接从空闲池获取,空闲时间
	RequestAttempt int           // 请求尝试次数
}

Res http响应

func ToResponse

func ToResponse(src *resty.Response) *Res

ToResponse 将 resty.Response 转为 Response 这个函数是为了说明两个结构体的关系

func (*Res) ToError

func (r *Res) ToError() error

ToError 请求失败时,返回错误

type Response

type Response = resty.Response

Jump to

Keyboard shortcuts

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