httpreq

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete added in v0.6.1

func Delete(ctx context.Context, url string, headers, querys map[string]string, bodys any) (res []byte, err error)

Delete请求

  • {bodys} 请求体, 支持 string, []byte, map[string]interface{}, io.Reader 类型, 不需要可传 nil

func Get added in v0.6.1

func Get(ctx context.Context, url string, headers, querys map[string]string) (res []byte, err error)

Get请求

func Post added in v0.6.1

func Post(ctx context.Context, url string, headers, querys map[string]string, bodys any) (res []byte, err error)

Post请求

  • {bodys} 请求体, 支持 string, []byte, map[string]interface{}, io.Reader 类型, 不需要可传 nil

func Put added in v0.6.1

func Put(ctx context.Context, url string, headers, querys map[string]string, bodys any) (res []byte, err error)

Put请求

  • {bodys} 请求体, 支持 string, []byte, map[string]interface{}, io.Reader 类型, 不需要可传 nil

func WithHeaders added in v0.6.1

func WithHeaders(headers map[string]string) reqOpt

WithHeaders 设置单次请求头

func WithQuerys added in v0.6.1

func WithQuerys(querys map[string]string) reqOpt

WithQuerys 设置请求查询参数

Types

type HttpReq added in v0.6.1

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

func New

func New(safe ...bool) *HttpReq

创建实例

  • {safe} 并发安全

func (*HttpReq) Delete added in v0.6.1

func (hr *HttpReq) Delete(apiOrUrl string, bodys any, opts ...reqOpt) (res []byte, err error)

Delete请求

  • {apiOrUrl} 请求Api或地址
  • {bodys} 请求体参数(body params), 支持 string, []byte, map[string]interface{}, io.Reader 类型, 不需要请传nil
  • {opts} 可选设置, 单次请求头(header params), 请求查询参数(query params)

func (*HttpReq) Get added in v0.6.1

func (hr *HttpReq) Get(apiOrUrl string, bodys any, opts ...reqOpt) (res []byte, err error)

Get请求

  • {apiOrUrl} 请求Api或地址
  • {bodys} 请求体参数(body params), 支持 string, []byte, map[string]interface{}, io.Reader 类型, 不需要请传nil
  • {opts} 可选设置, 单次请求头(header params), 请求查询参数(query params)

func (*HttpReq) Post added in v0.6.1

func (hr *HttpReq) Post(apiOrUrl string, bodys any, opts ...reqOpt) (res []byte, err error)

Post请求

  • {apiOrUrl} 请求Api或地址
  • {bodys} 请求体参数(body params), 支持 string, []byte, map[string]interface{}, io.Reader 类型, 不需要请传nil
  • {opts} 可选设置, 单次请求头(header params), 请求查询参数(query params)

func (*HttpReq) Put added in v0.6.1

func (hr *HttpReq) Put(apiOrUrl string, bodys any, opts ...reqOpt) (res []byte, err error)

Put请求

  • {apiOrUrl} 请求Api或地址
  • {bodys} 请求体参数(body params), 支持 string, []byte, map[string]interface{}, io.Reader 类型, 不需要请传nil
  • {opts} 可选设置, 单次请求头(header params), 请求查询参数(query params)

func (*HttpReq) SetBasicAuth added in v0.6.1

func (hr *HttpReq) SetBasicAuth(username, password string) *HttpReq

SetBasicAuth 设置 HTTP 基本认证

func (*HttpReq) SetContext added in v0.6.1

func (hr *HttpReq) SetContext(ctx context.Context) *HttpReq

SetContext 设置上下文

func (*HttpReq) SetDomain added in v0.6.1

func (hr *HttpReq) SetDomain(domain string) *HttpReq

SetDomain 设置请求域

func (*HttpReq) SetHeaders added in v0.6.1

func (hr *HttpReq) SetHeaders(headers map[string]string) *HttpReq

SetHeaders 设置全局请求头

func (*HttpReq) SetTimeout added in v0.6.1

func (hr *HttpReq) SetTimeout(t time.Duration) *HttpReq

SetTimeout 设置总超时时间

比较:

a. 总超时时间(client.Timeout) 用于所有请求,会被请求超时时间覆盖,从 Client.Do() 开始计时
b. 请求超时时间(context.WithTimeout) 优先级更高,支持动态取消,从 context 创建/传递开始计时

注: 总超时时间 = 连接时间 + 重定向时间 + 读取响应时间

func (*HttpReq) SetTlsClientVerify added in v0.6.1

func (hr *HttpReq) SetTlsClientVerify(certFilePath, keyFilePath string, keySecret ...string) *HttpReq

SetTlsClientVerify 设置客户端Tls证书校验 (双向认证)

  • {certFilePath} 公钥, 支持非PEM结构, xxx.crt/cert.pem (publicKey.pem)
  • {keyFilePath} 私钥, 支持非PEM结构, xxx.key/key.pem (privateKey.pem)
  • {keySecret} 可选,若私钥被加密需要提供密码

func (*HttpReq) SetTlsServerVerify added in v0.6.1

func (hr *HttpReq) SetTlsServerVerify(caFilePath string) *HttpReq

SetTlsServerVerify 设置服务端Tls证书校验 (自签证书校验)

  • {caFilePath} 服务端证书, xxx.crt/ca.crt

func (*HttpReq) SetTlsSkipServerVerify added in v0.6.1

func (hr *HttpReq) SetTlsSkipServerVerify() *HttpReq

SetTlsSkipServerVerify 设置服务端Tls证书跳过校验

Jump to

Keyboard shortcuts

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