ghttp

package
v0.1.39 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const CAT_POST_LEN_LIMIT = 200
View Source
const CTX_TRACEDATA_KEY = "tracedata"
View Source
const DEFAULT_TIMEOUT = 3000

默认超时时间

Variables

This section is empty.

Functions

func Do

func Do(ctx context.Context, rawRequest *http.Request, config *HttpConfig) (rawResponse *http.Response, err error)

通用请求(参考标准库Do方法),但是支持CAT以及部分通用的请求配置项

func InitHttpClient

func InitHttpClient(config *HttpConfig) (err error)

初始化http配置

Types

type HttpConfig

type HttpConfig struct {
	Timeout int `toml:"timeout"` // 连接超时时间
	Header  map[string]string
	Cookie  []*http.Cookie
}

请求配置

type Request

type Request struct {
	RawReq *http.Request // 原始请求
	Config *HttpConfig   // 请求级配置

	Form *url.Values // form传参
	// contains filtered or unexported fields
}

请求封装

func NewDoRequest

func NewDoRequest(rawReq *http.Request, config *HttpConfig) (req *Request, err error)

创建Do请求

func NewGetRequest

func NewGetRequest(reqUrl string, query *url.Values, config *HttpConfig) (req *Request, err error)

创建GET请求

func NewPostFileRequest

func NewPostFileRequest(reqUrl string, query *url.Values, form *url.Values, files map[string]io.Reader, config *HttpConfig) (req *Request, err error)

创建POST FILE请求

func NewPostJsonRequest

func NewPostJsonRequest(reqUrl string, query *url.Values, json []byte, config *HttpConfig) (req *Request, err error)

创建POST JSON请求

func NewPostRequest

func NewPostRequest(reqUrl string, query *url.Values, form *url.Values, config *HttpConfig) (req *Request, err error)

创建POST表单请求

type Response

type Response struct {
	RawResp *http.Response // 原始应答
	Body    []byte         // 应答体
	Err     error          // 错误
}

应答封装

func Get

func Get(ctx context.Context, reqUrl string, query *url.Values, config *HttpConfig) (response *Response, err error)

GET请求

func MultiRequest

func MultiRequest(ctx context.Context, reqList []*Request) (respList []*Response, err error)

并发调用

func Post

func Post(ctx context.Context, reqUrl string, query *url.Values, form *url.Values, config *HttpConfig) (response *Response, err error)

POST表单

func PostFile

func PostFile(ctx context.Context, reqUrl string, query *url.Values, form *url.Values, files map[string]io.Reader, config *HttpConfig) (response *Response, err error)

POST FILE

func PostJson

func PostJson(ctx context.Context, reqUrl string, query *url.Values, json []byte, config *HttpConfig) (response *Response, err error)

POST JSON

type TraceData

type TraceData struct {
	Host       string // 正在发起HTTP调用客户端的DOMAIN
	RequestURI string // 正在发起HTTP调用的客户端URL
	TraceStr   string // X-Trace-Str
	TraceID    string // X-Request-Id
}

请求链路元数据

Jump to

Keyboard shortcuts

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