xhttp

package
v0.0.0-...-cf612d8 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2025 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ContentType           = "Content-Type"
	ContentTypeHTML       = "text/html"
	ContentTypeJSON       = "application/json"
	ContentTypeXML        = "application/xml"
	ContentTypeYAML       = "application/x-yaml"
	ContentTypeText       = "text/plain"
	ContentTypeURLEncoded = "application/x-www-form-urlencoded"
)

Variables

This section is empty.

Functions

func BasicAuth

func BasicAuth(username, password string) xopt.Option[Request]

func BodyJSON

func BodyJSON[T any](body T) xopt.Option[Request]

func BodyMultipartForm

func BodyMultipartForm(form *multipart.Form) xopt.Option[Request]

func BodyText

func BodyText(body string) xopt.Option[Request]

func BodyWWWFormURLEncoded

func BodyWWWFormURLEncoded(body url.Values) xopt.Option[Request]

func BodyXML

func BodyXML[T any](body T) xopt.Option[Request]

func BodyYAML

func BodyYAML[T any](body T) xopt.Option[Request]

func CheckRedirect

func CheckRedirect(f func(req *http.Request, via []*http.Request) error) xopt.Option[Request]

func Client

func Client(client *http.Client) xopt.Option[Request]

func DecoderJSON

func DecoderJSON() xopt.Option[Request]

func DecoderText

func DecoderText() xopt.Option[Request]

func DecoderXML

func DecoderXML() xopt.Option[Request]

func DecoderYAML

func DecoderYAML() xopt.Option[Request]
func Header(key, value string, replace ...bool) xopt.Option[Request]

func Headers

func Headers(headers http.Header, replace ...bool) xopt.Option[Request]

func Jar

func Jar(jar http.CookieJar) xopt.Option[Request]

func Method

func Method(method string) xopt.Option[Request]

func NativeBody

func NativeBody(contentType string, body io.Reader) xopt.Option[Request]

func RegisterInterceptors

func RegisterInterceptors(i ...Interceptor)

RegisterInterceptor 注册拦截器

func Timeout

func Timeout(timeout time.Duration) xopt.Option[Request]

func Transport

func Transport(transport http.RoundTripper) xopt.Option[Request]

func URLRawQuery

func URLRawQuery(query url.Values) xopt.Option[Request]

Types

type Interceptor

type Interceptor interface {
	// 在请求发送前处理
	Before(req *http.Request) error
	// 在响应返回后处理
	After(resp *http.Response, req *http.Request) error
}

Interceptor 定义拦截器接口

type InterceptorFunc

type InterceptorFunc struct {
	BeforeFunc func(req *http.Request) error
	AfterFunc  func(resp *http.Response, req *http.Request) error
}

InterceptorFunc 适配函数式拦截器

func (*InterceptorFunc) After

func (f *InterceptorFunc) After(resp *http.Response, req *http.Request) error

func (*InterceptorFunc) Before

func (f *InterceptorFunc) Before(req *http.Request) error

type Request

type Request struct {
	*http.Request
	*http.Client
	// contains filtered or unexported fields
}

type Response

type Response[T any] struct {
	Body T   `json:"body" xml:"body" yaml:"body"`
	Code int `json:"code" xml:"code" yaml:"code"`

	*Request     `json:"-" xml:"-" yaml:"-"`
	*TraceResult `json:"trace_result" xml:"trace_result" yaml:"trace_result"`
}

func Do

func Do(ctx context.Context, url string, opts ...xopt.Option[Request]) (*Response[[]byte], error)

func Sugar

func Sugar[T any](ctx context.Context, url string, opts ...xopt.Option[Request]) (*Response[T], error)

func (*Response[T]) PrettyString

func (r *Response[T]) PrettyString() string

type TraceResult

type TraceResult struct {
	TotalDuration   Counter[time.Duration] `json:"total_duration"`
	DNSDuration     Counter[time.Duration] `json:"dns_duration"`
	ConnectDuration Counter[time.Duration] `json:"connect_duration"`
	TlsDuration     Counter[time.Duration] `json:"tls_duration"`
	FirstByteDelay  Counter[time.Duration] `json:"first_byte_delay"`
	// contains filtered or unexported fields
}

TraceResult 存储追踪结果

func (*TraceResult) FormatCounter

func (tr *TraceResult) FormatCounter()

Jump to

Keyboard shortcuts

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