httpx

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ContentRaw     = "Raw"            //原始请求
	ContentForm    = "Form"           //Form请求
	ContentJson    = "Json"           //Json请求
	DefaultTimeout = 30 * time.Second //默认请求时间
)
View Source
const (
	MethodGet     = http.MethodGet
	MethodHead    = http.MethodHead
	MethodPost    = http.MethodPost
	MethodPut     = http.MethodPut
	MethodPatch   = http.MethodPatch // RFC 5789
	MethodDelete  = http.MethodDelete
	MethodConnect = http.MethodConnect
	MethodOptions = http.MethodOptions
	MethodTrace   = http.MethodTrace
)

Variables

This section is empty.

Functions

func FormatFormData

func FormatFormData(data map[string]string) string

FormatFormData 格式化form数据

func FormatJsonData

func FormatJsonData(data interface{}) string

FormatJsonData 格式化json数据,支持任意类型

func WithBasicAuth

func WithBasicAuth(auth *RequestBasicAuth) opt.Option[requestOptions]

Types

type Client

type Client interface {
	Send(ctx context.Context, req *Request, opts ...opt.Option[requestOptions]) (resp *Response, err error)
}

func New

func New(conf *Config) Client

type Config

type Config struct {
	Timeout time.Duration
	Trace   bool
}

type Request

type Request struct {
	Url         string            `json:"url"`          // 请求url
	Method      string            `json:"method"`       //请求方法,GET/POST/PUT/DELETE/PATCH...
	Params      map[string]string `json:"params"`       //Query参数
	Body        string            `json:"body"`         //请求体
	Headers     map[string]string `json:"headers"`      // 请求头
	ContentType string            `json:"content_type"` //数据编码格式 //TODO:更多
	Files       map[string]string `json:"files"`        //TODO:文件
	Cookies     map[string]string `json:"cookies"`      //Cookies

}

Request 请求结构体

type RequestBasicAuth

type RequestBasicAuth struct {
	Username string `json:"username"` //base认证username
	Password string `json:"password"` //base认证password
}

type Response

type Response struct {
	StatusCode int               `json:"status_code"` // 状态码
	Success    bool              `json:"success"`     // 响应状态
	Content    []byte            `json:"content"`     // 响应内容-字节
	Reason     string            `json:"reason"`      // 状态码说明
	Elapsed    float64           `json:"elapsed"`     // 请求耗时(秒)
	Headers    map[string]string `json:"headers"`     // 响应头
	Cookies    map[string]string `json:"cookies"`     // 响应Cookies
	Request    *Request          `json:"request"`     // 原始请求
}

Response 响应结构体

Jump to

Keyboard shortcuts

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