gorequest

package module
v1.0.89 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2024 License: MIT Imports: 25 Imported by: 35

Documentation

Index

Constants

View Source
const Version = "1.0.89"

Variables

View Source
var (
	XRequestID = "X-Request-ID"
	TNil       = "%!s(<nil>)"
)
View Source
var (
	TraceID = "trace-Id"
)

Functions

func ClientIp

func ClientIp(r *http.Request) string

ClientIp 尽最大努力实现获取客户端 IP 的算法。 解析 X-Real-IP 和 X-Forwarded-For 以便于反向代理(nginx 或 haproxy)可以正常工作。

func DeCode

func DeCode(s string) string

DeCode 解码

func GetCmdOutsideIP

func GetCmdOutsideIP() string

GetCmdOutsideIP 通过命令获取外网IP

func GetInsideIp

func GetInsideIp(ctx context.Context) string

GetInsideIp 内网IP

func GetMacAddr

func GetMacAddr(ctx context.Context) (arrays []string)

GetMacAddr 获取Mac地址

func GetOutsideIPV4All

func GetOutsideIPV4All(ctx context.Context) string

GetOutsideIPV4All 外网IPV4地址

func GetOutsideIPV6All

func GetOutsideIPV6All(ctx context.Context) string

GetOutsideIPV6All 外网IPV6地址

func GetOutsideIp

func GetOutsideIp(ctx context.Context) string

GetOutsideIp 外网IP

func GetParamsString

func GetParamsString(src interface{}) string

GetParamsString 获取参数字符串

func GetRandomUserAgent

func GetRandomUserAgent() string

GetRandomUserAgent 获取随机UA

func GetRandomUserAgentSystem

func GetRandomUserAgentSystem() string

GetRandomUserAgentSystem 获取系统随机UA

func GetRequestIDContext

func GetRequestIDContext(ctx context.Context) string

GetRequestIDContext 获取请求编号

func IpIs

func IpIs(ipStr string) string

IpIs 是否ip

func IpIsConsistent

func IpIsConsistent(ipStr1, ipStr2 string) bool

IpIsConsistent 两个ip是否一致

func Ips

func Ips(ctx context.Context) (map[string]string, error)

Ips 获取全部网卡的全部IP

func IsHttpURL added in v1.0.84

func IsHttpURL(s string) bool

func IsIPV4

func IsIPV4(s string) bool

func IsIPV6

func IsIPV6(s string) bool

func IsIPv4Public

func IsIPv4Public(ip net.IP) bool

func IsIPv6Public

func IsIPv6Public(ip net.IP) bool

func IsURL added in v1.0.84

func IsURL(s string) bool

func IsWechatMiniProgramRequest

func IsWechatMiniProgramRequest(r *http.Request, appid string) error

IsWechatMiniProgramRequest 判断是否是微信小程序

func LenCode

func LenCode(s string) string

LenCode 编码

func ParseQuery

func ParseQuery(s string) map[string][]string

ParseQuery 获取URL参数 https://studygolang.com/articles/2876

func SetRequestIDContext

func SetRequestIDContext(ctx context.Context) context.Context

SetRequestIDContext 设置请求编号

func ToXml

func ToXml(params map[string]any) (reader io.Reader, err error)

Types

type App

type App struct {
	Uri string // 全局请求地址,没有设置url才会使用
	// contains filtered or unexported fields
}

App 实例

func NewHttp

func NewHttp() *App

NewHttp 实例化

func (*App) Connect

func (c *App) Connect(ctx context.Context, uri ...string) (Response, error)

Connect 发起 CONNECT 请求

func (*App) Delete

func (c *App) Delete(ctx context.Context, uri ...string) (Response, error)

Delete 发起 DELETE 请求

func (*App) Get

func (c *App) Get(ctx context.Context, uri ...string) (Response, error)

Get 发起 GET 请求

func (*App) Head

func (c *App) Head(ctx context.Context, uri ...string) (Response, error)

Head 发起 HEAD 请求

func (*App) Options

func (c *App) Options(ctx context.Context, uri ...string) (Response, error)

Options 发起 OPTIONS 请求

func (*App) Patch

func (c *App) Patch(ctx context.Context, uri ...string) (Response, error)

Patch 发起 PATCH 请求

func (*App) Post

func (c *App) Post(ctx context.Context, uri ...string) (Response, error)

Post 发起 POST 请求

func (*App) Put

func (c *App) Put(ctx context.Context, uri ...string) (Response, error)

Put 发起 PUT 请求

func (*App) Request

func (c *App) Request(ctx context.Context) (Response, error)

Request 发起请求

func (*App) SetAuthToken

func (c *App) SetAuthToken(token string)

SetAuthToken 设置身份验证令牌

func (*App) SetClientIP

func (c *App) SetClientIP(clientIP string)

SetClientIP 设置客户端IP

func (*App) SetContentTypeForm

func (c *App) SetContentTypeForm()

SetContentTypeForm 设置FORM格式

func (*App) SetContentTypeJson

func (c *App) SetContentTypeJson()

SetContentTypeJson 设置JSON格式

func (*App) SetContentTypeXml

func (c *App) SetContentTypeXml()

SetContentTypeXml 设置XML格式

func (*App) SetCookie

func (c *App) SetCookie(cookie string)

SetCookie 设置Cookie

func (*App) SetHeader

func (c *App) SetHeader(key, value string)

SetHeader 设置请求头

func (*App) SetHeaders

func (c *App) SetHeaders(headers *Headers)

SetHeaders 批量设置请求头

func (*App) SetLogFunc

func (c *App) SetLogFunc(logFunc LogFunc)

SetLogFunc 设置日志记录方法

func (*App) SetMethod

func (c *App) SetMethod(method string)

SetMethod 设置请求方式

func (*App) SetP12Cert

func (c *App) SetP12Cert(content *tls.Certificate)

SetP12Cert 设置证书

func (*App) SetParam

func (c *App) SetParam(key string, value interface{})

SetParam 设置请求参数

func (*App) SetParams

func (c *App) SetParams(params *Params)

SetParams 批量设置请求参数

func (*App) SetTlsVersion

func (c *App) SetTlsVersion(minVersion, maxVersion uint16)

SetTlsVersion 设置TLS版本

func (*App) SetUri

func (c *App) SetUri(uri string)

SetUri 设置请求地址

func (*App) SetUserAgent

func (c *App) SetUserAgent(ua string)

SetUserAgent 设置用户代理,传空字符串就随机设置

func (*App) Trace

func (c *App) Trace(ctx context.Context, uri ...string) (Response, error)

Trace 发起 TRACE 请求

type Headers

type Headers struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Headers 头部信息

func NewHeaders

func NewHeaders() *Headers

NewHeaders 新建头部信息

func NewNewHeadersWith

func NewNewHeadersWith(headers ...*Headers) *Headers

NewNewHeadersWith 头部信息使用

func (*Headers) DeepCopy

func (h *Headers) DeepCopy() map[string]string

DeepCopy 深度复制

func (*Headers) DeepCopyNew added in v1.0.89

func (h *Headers) DeepCopyNew() *Headers

DeepCopyNew 深度复制

func (*Headers) DeepGet added in v1.0.88

func (h *Headers) DeepGet() map[string]string

DeepGet 深度获取

func (*Headers) Get added in v1.0.87

func (h *Headers) Get(key string) string

Get 获取头部信息

func (*Headers) GetQuery

func (h *Headers) GetQuery() string

GetQuery 获取头部信息

func (*Headers) Set

func (h *Headers) Set(key string, value string)

Set 设置头部信息

func (*Headers) SetHeaders

func (h *Headers) SetHeaders(headers *Headers)

SetHeaders 批量设置头部信息

type LogFunc

type LogFunc func(ctx context.Context, response *LogResponse)

LogFunc 日志函数

type LogResponse

type LogResponse struct {
	TraceID string `json:"trace_id"` // 追踪编号

	RequestID       string              `json:"request_id"`        // 请求编号
	RequestTime     time.Time           `json:"request_time"`      // 请求时间
	RequestHost     string              `json:"request_host"`      // 请求主机
	RequestPath     string              `json:"request_path"`      // 请求地址
	RequestQuery    map[string][]string `json:"request_query"`     // 请求参数
	RequestMethod   string              `json:"request_method"`    // 请求方式
	RequestBody     map[string]any      `json:"request_body"`      // 请求内容
	RequestIP       string              `json:"request_ip"`        // 请求IP
	RequestHeader   map[string][]string `json:"request_header"`    // 请求头
	RequestCostTime int64               `json:"request_cost_time"` // 请求消耗时长

	ResponseTime     time.Time           `json:"response_time"`      // 返回时间
	ResponseHeader   map[string][]string `json:"response_header"`    // 返回头部
	ResponseCode     int                 `json:"response_code"`      // 返回状态码
	ResponseBody     string              `json:"response_body"`      // 返回Json数据
	ResponseBodyJson map[string]any      `json:"response_body_json"` // 返回Json数据
	ResponseBodyXml  string              `json:"response_body_xml"`  // 返回Xml数据

}

type OldHeaders added in v1.0.88

type OldHeaders map[string]string

OldHeaders 头部信息

func NewNewOldHeadersWith added in v1.0.88

func NewNewOldHeadersWith(oldHeaders ...OldHeaders) OldHeaders

NewNewOldHeadersWith 头部信息使用

func NewOldHeaders added in v1.0.88

func NewOldHeaders() OldHeaders

NewOldHeaders 新建头部信息

func (*OldHeaders) DeepCopy added in v1.0.88

func (h *OldHeaders) DeepCopy() map[string]string

DeepCopy 深度复制

func (OldHeaders) GetQuery added in v1.0.88

func (h OldHeaders) GetQuery() string

GetQuery 获取头部信息

func (OldHeaders) Set added in v1.0.88

func (h OldHeaders) Set(key, value string)

Set 设置头部信息

func (OldHeaders) SetHeaders added in v1.0.88

func (h OldHeaders) SetHeaders(OldHeaders OldHeaders)

SetHeaders 批量设置头部信息

type OldParams added in v1.0.88

type OldParams map[string]any

OldParams 参数

func NewOldParams added in v1.0.88

func NewOldParams() OldParams

NewOldParams 新建参数

func NewOldParamsWith added in v1.0.88

func NewOldParamsWith(oldParams ...OldParams) OldParams

NewOldParamsWith 参数使用

func (*OldParams) DeepCopy added in v1.0.88

func (p *OldParams) DeepCopy() map[string]any

DeepCopy 深度复制

func (OldParams) Get added in v1.0.88

func (p OldParams) Get(key string) interface{}

Get 获取参数

func (OldParams) Set added in v1.0.88

func (p OldParams) Set(key string, value interface{})

Set 设置参数

func (OldParams) SetParams added in v1.0.88

func (p OldParams) SetParams(OldParams OldParams)

SetParams 批量设置参数

type Params

type Params struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Params 参数

func NewParams

func NewParams() *Params

NewParams 新建参数

func NewParamsWith

func NewParamsWith(params ...*Params) *Params

NewParamsWith 参数使用

func (*Params) DeepCopy

func (p *Params) DeepCopy() map[string]any

DeepCopy 深度复制

func (*Params) DeepCopyNew added in v1.0.89

func (p *Params) DeepCopyNew() *Params

DeepCopyNew 深度复制

func (*Params) DeepGet added in v1.0.88

func (p *Params) DeepGet() map[string]any

DeepGet 深度获取

func (*Params) Get

func (p *Params) Get(key string) any

Get 获取参数

func (*Params) Set

func (p *Params) Set(key string, value any)

Set 设置参数

func (*Params) SetParams

func (p *Params) SetParams(params *Params)

SetParams 批量设置参数

type Response

type Response struct {
	RequestID             string      `json:"request_id"`              // 请求编号
	RequestUri            string      `json:"request_uri"`             // 请求链接
	RequestParams         *Params     `json:"request_params"`          // 请求参数
	RequestMethod         string      `json:"request_method"`          // 请求方式
	RequestHeader         *Headers    `json:"request_header"`          // 请求头部
	RequestCookie         string      `json:"request_cookie"`          // 请求Cookie
	RequestTime           time.Time   `json:"request_time"`            // 请求时间
	RequestCostTime       int64       `json:"request_cost_time"`       // 请求消耗时长
	ResponseHeader        http.Header `json:"response_header"`         // 响应头部
	ResponseStatus        string      `json:"response_status"`         // 响应状态
	ResponseStatusCode    int         `json:"response_status_code"`    // 响应状态码
	ResponseBody          []byte      `json:"response_body"`           // 响应内容
	ResponseContentLength int64       `json:"response_content_length"` // 响应大小
	ResponseTime          time.Time   `json:"response_time"`           // 响应时间
}

Response 返回内容

func (*Response) HeaderHtml

func (r *Response) HeaderHtml() bool

HeaderHtml 判断是否为Html

func (*Response) HeaderIsImg

func (r *Response) HeaderIsImg() bool

HeaderIsImg 判断是否为图片

func (*Response) HeaderIsJpeg

func (r *Response) HeaderIsJpeg() bool

HeaderIsJpeg 判断是否为jpeg图片

func (*Response) HeaderIsJpg

func (r *Response) HeaderIsJpg() bool

HeaderIsJpg 判断是否为Jpg图片

func (*Response) HeaderIsPng

func (r *Response) HeaderIsPng() bool

HeaderIsPng 判断是否为Png图片

func (*Response) HeaderJson

func (r *Response) HeaderJson() bool

HeaderJson 判断是否为Json数据

func (*Response) HeaderTextHtml

func (r *Response) HeaderTextHtml() bool

HeaderTextHtml 判断是否为Html

func (*Response) HeaderXHtml

func (r *Response) HeaderXHtml() bool

HeaderXHtml 判断是否为Html

type ResponseUrlParse

type ResponseUrlParse struct {
	Uri      string `json:"uri"`       // URI
	Urn      string `json:"urn"`       // URN
	Url      string `json:"url"`       // URL
	Scheme   string `json:"scheme"`    // 协议
	Host     string `json:"host"`      // 主机
	Hostname string `json:"hostname"`  // 主机名
	Port     string `json:"port"`      // 端口
	Path     string `json:"path"`      // 路径
	RawQuery string `json:"raw_query"` // 参数 ?
	Fragment string `json:"fragment"`  // 片段 #
}

ResponseUrlParse 返回参数

type UriParse

type UriParse struct {
	// contains filtered or unexported fields
}

func NewUri

func NewUri(uri string) *UriParse

func (*UriParse) Parse

func (u *UriParse) Parse() (resp ResponseUrlParse)

Parse 解析URl

func (*UriParse) UriFilterExcludeQueryString

func (u *UriParse) UriFilterExcludeQueryString() string

UriFilterExcludeQueryString 过滤掉url中的参数

Jump to

Keyboard shortcuts

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