httpclient

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2023 License: MulanPSL-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package httpclient 提供简单易用的http library 包装自 github.com/go-resty/resty

Index

Constants

This section is empty.

Variables

View Source
var (
	//header key
	HdrUserAgentKey       = http.CanonicalHeaderKey("User-Agent")
	HdrAcceptKey          = http.CanonicalHeaderKey("Accept")
	HdrContentTypeKey     = http.CanonicalHeaderKey("Content-Type")
	HdrContentLengthKey   = http.CanonicalHeaderKey("Content-Length")
	HdrContentEncodingKey = http.CanonicalHeaderKey("Content-Encoding")
	HdrAuthorizationKey   = http.CanonicalHeaderKey("Authorization")

	PlainTextType   = "text/plain; charset=utf-8"
	JSONContentType = "application/json"
	FormContentType = "application/x-www-form-urlencoded"
)

Functions

This section is empty.

Types

type HTTPClient

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

HTTPClient resty.Client的封装

func New

func New() *HTTPClient

New 初始化HTTPClient

func NewByApiKey

func NewByApiKey(apiKey string) *HTTPClient

指定监控apikey, 例如下载图片,否则每次请求启动一个monitorResponseTime, 导致内存泄露

func NewHTTPClient

func NewHTTPClient(transport *http.Transport) *HTTPClient

func (*HTTPClient) OnAfterResponseHook

func (hc *HTTPClient) OnAfterResponseHook(f resty.ResponseMiddleware) *HTTPClient

OnAfterResponseHook 装载响应后置钩子(响应报err不会执行到此hook)

func (*HTTPClient) OnBeforeRequestHook

func (hc *HTTPClient) OnBeforeRequestHook(f resty.RequestMiddleware) *HTTPClient

OnBeforeRequestHook 装载请求前置钩子

func (*HTTPClient) Request

func (hc *HTTPClient) Request() *HTTPRequest

Request 初始化

func (*HTTPClient) SetDebug

func (hc *HTTPClient) SetDebug(d bool) *HTTPClient

func (*HTTPClient) SetFormData

func (hc *HTTPClient) SetFormData(data map[string]string) *HTTPClient

SetFormData 设置Form data Request SetFormData可以覆盖此设置

func (*HTTPClient) SetHeader

func (hc *HTTPClient) SetHeader(header, value string) *HTTPClient

SetHeader 设置Header, Request SetHeader可以覆盖此设置

func (*HTTPClient) SetHeaders

func (hc *HTTPClient) SetHeaders(headers map[string]string) *HTTPClient

SetHeaders 设置多个Header字段, Request SetHeaders可以覆盖此设置 一个client instance 可以发起多个Request

func (*HTTPClient) SetHostURL

func (hc *HTTPClient) SetHostURL(url string) *HTTPClient

SetHostURL 设置host url Setting HTTP address SetHostURL("http://babytree.com") Setting HTTPS address SetHostURL("https://babytree.com")

func (*HTTPClient) SetLogger

func (hc *HTTPClient) SetLogger(l resty.Logger) *HTTPClient

func (*HTTPClient) SetPathParams

func (hc *HTTPClient) SetPathParams(params map[string]string) *HTTPClient

SetPathParams 设置动态url path, Request SetPathParams可以覆盖此设置

func (*HTTPClient) SetPreRequestHook

func (hc *HTTPClient) SetPreRequestHook(f resty.PreRequestHook) *HTTPClient

func (*HTTPClient) SetQueryParam

func (hc *HTTPClient) SetQueryParam(param, value string) *HTTPClient

SetQueryParam 设置query参数, Request SetQueryParam可以覆盖此设置 一个client instance 可以发起多个Request,一般公共参数用此方法

func (*HTTPClient) SetQueryParams

func (hc *HTTPClient) SetQueryParams(params map[string]string) *HTTPClient

SetQueryParams 设置多个query参数, Request SetQueryParams可以覆盖此设置

func (*HTTPClient) SetTimeout

func (hc *HTTPClient) SetTimeout(timeout time.Duration) *HTTPClient

SetTimeout 设置超时

func (*HTTPClient) SetTransport

func (hc *HTTPClient) SetTransport(transport *http.Transport) *HTTPClient

SetTransport 设置http.Transport

type HTTPRequest

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

HTTPRequest resty.Request的封装

func (*HTTPRequest) EnableLogging

func (hr *HTTPRequest) EnableLogging() *HTTPRequest

EnableLogging 开启日志

func (*HTTPRequest) EnableTrace

func (hr *HTTPRequest) EnableTrace() *HTTPRequest

func (*HTTPRequest) Get

func (hr *HTTPRequest) Get(url string) (*resty.Response, error)

Get http get 请求

func (*HTTPRequest) OnAfterResponseHook

func (hr *HTTPRequest) OnAfterResponseHook(f resty.ResponseMiddleware) *HTTPRequest

OnAfterResponse 响应后置钩子函数, 区别于client的 OnAfterResponseHook OnAfterResponse可以在响应报err后继续执行 OnAfterResponse的执行顺序要后于client的 OnAfterResponseHook 响应成功的后置操作建议使用client的 OnAfterResponseHook,需要响应失败的后置操作使用此函数

func (*HTTPRequest) Post

func (hr *HTTPRequest) Post(url string) (*resty.Response, error)

Post http post 请求

func (*HTTPRequest) SetBody

func (hr *HTTPRequest) SetBody(body interface{}) *HTTPRequest

SetBody 设置body参数 支持 JSON string SetBody(`{"username":"testuser", "password":"testpass"}`) 支持 []byte array SetBody([]byte(`{"username":"testuser", "password":"testpass"}`)) 支持 Struct SetBody(User{Username: "testuser", Password: "testpass"}) 支持 Map SetBody(map[string]interface{}{"username": "testuser", "password": "testpass"}) 注 Content-Type为json格式时,支持以上四项,Content-Type为xml时,仅支持后两项,因为Struct和Map方式需要序列化支持

func (*HTTPRequest) SetContentLength

func (hr *HTTPRequest) SetContentLength(l bool) *HTTPRequest

SetContentLength 是否需要设置header中ContentLength的值

func (*HTTPRequest) SetFile

func (hr *HTTPRequest) SetFile(filename, filepath string) *HTTPRequest

SetFile 设置文件名称与路径用于上传, 自动设置Content-Type为multipart/form-data

func (*HTTPRequest) SetFiles

func (hr *HTTPRequest) SetFiles(files map[string]string) *HTTPRequest

SetFiles 设置多个文件信息用于上传

SetFiles(map[string]string{
			"my_file1": "/Users/jeeva/Gas Bill - Sep.pdf",
			"my_file2": "/Users/jeeva/Electricity Bill - Sep.pdf",
			"my_file3": "/Users/jeeva/Water Bill - Sep.pdf",
})

func (*HTTPRequest) SetFormData

func (hr *HTTPRequest) SetFormData(data map[string]string) *HTTPRequest

SetFormData 设置表单参数,仅支持Post, 自动设置Content-Type为application/x-www-form-urlencoded

func (*HTTPRequest) SetHeader

func (hr *HTTPRequest) SetHeader(header, value string) *HTTPRequest

SetHeader 设置Header

func (*HTTPRequest) SetHeaderV2

func (hr *HTTPRequest) SetHeaderV2(header, value string) *HTTPRequest

SetHeader 设置Header

func (*HTTPRequest) SetHeaders

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

SetHeaders 设置多个Header字段

func (*HTTPRequest) SetHeadersV2

func (hr *HTTPRequest) SetHeadersV2(headers map[string]string) *HTTPRequest

SetHeaders 设置多个Header字段

func (*HTTPRequest) SetPathParams

func (hr *HTTPRequest) SetPathParams(params map[string]string) *HTTPRequest

SetPathParams 设置动态url path

func (*HTTPRequest) SetQueryParam

func (hr *HTTPRequest) SetQueryParam(param, value string) *HTTPRequest

SetQueryParam 设置query参数

func (*HTTPRequest) SetQueryParams

func (hr *HTTPRequest) SetQueryParams(params map[string]string) *HTTPRequest

SetQueryParams 设置多个query参数

func (*HTTPRequest) SetResult

func (hr *HTTPRequest) SetResult(res interface{}) *HTTPRequest

SetResult 如果响应状态码在200到299之间且Content-Type为application/json或Type为application/xml,设置自动解码对象 注:res 可以是指针或非指针

func (*HTTPRequest) TraceInfo

func (hr *HTTPRequest) TraceInfo() resty.TraceInfo

type RequestTimeRecord

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

监控记录

Jump to

Keyboard shortcuts

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