Documentation ¶
Index ¶
- Variables
- func DownloadFile(url string, progress bool, filepath ...string) error
- func DumpRequest(req *http.Request) ([]byte, error)
- func DumpRequestIndent(req *http.Request) string
- func Log(format string, v ...any)
- func NewRequestWithContext(ctx context.Context, options Options) (*http.Request, error)
- func Upload(url, field, file string) (*http.Response, error)
- func Wget(url, name string) (int, error)
- type Option
- func BasicAuth(user, pass string) Option
- func Body(body any) Option
- func Cookie(cookie http.Cookie) Option
- func Cookies(cookies ...http.Cookie) Option
- func Form(form url.Values) Option
- func Gzip(body any) Option
- func Header(k, v string) Option
- func Headers(kv map[string]string) Option
- func Hosts(hosts map[string][]string) Option
- func LocalAddr(addr net.Addr) Option
- func Logf(f func(context.Context, Stat)) Option
- func MaxConns(conn int) Option
- func Method(method string) Option
- func Param(k string, v any) Option
- func Params(query map[string]any) Option
- func Path(path string) Option
- func Proxy(addr string) Option
- func RequestEach(each ...func(context.Context, *http.Request) error) Option
- func ResponseEach(each ...func(context.Context, *http.Response) error) Option
- func Stream(stream func(int64, []byte) error) Option
- func Timeout(timeout time.Duration) Option
- func TraceLv(v int, limit ...int) Option
- func URL(url string) Option
- func Verify(verify bool) Option
- type Options
- type Response
- func Delete(url, contentType string, body io.Reader) (*Response, error)
- func Get(url string) (*Response, error)
- func Head(url string) (resp *Response, err error)
- func PUT(url, contentType string, body io.Reader) (*Response, error)
- func Post(url string, contentType string, body io.Reader) (*Response, error)
- func PostForm(url string, data url.Values) (*Response, error)
- func (resp *Response) Bytes() []byte
- func (resp *Response) Download(name string) (int, error)
- func (resp *Response) Dump() ([]byte, error)
- func (resp *Response) Error() string
- func (resp *Response) JSON(v any) error
- func (resp *Response) Stat() Stat
- func (resp *Response) StdLib() *http.Response
- func (resp *Response) String() string
- func (resp *Response) Text() string
- type Session
- func (s *Session) DebugTrace(req *http.Request, v int, limit int) (*http.Response, error)
- func (s *Session) Delete(url, contentType string, body io.Reader) (resp *Response, err error)
- func (s *Session) DeleteWithContext(ctx context.Context, url, contentType string, body io.Reader) (*Response, error)
- func (s *Session) Do(method, url, contentType string, body io.Reader) (*Response, error)
- func (s *Session) DoRequest(ctx context.Context, opts ...Option) (*Response, error)
- func (s *Session) DoWithContext(ctx context.Context, method, url, contentType string, body io.Reader) (*Response, error)
- func (s *Session) Get(url string) (*Response, error)
- func (s *Session) GetWithContext(ctx context.Context, url string) (*Response, error)
- func (s *Session) Head(url string) (*Response, error)
- func (s *Session) Init(opts ...Option)
- func (s *Session) Post(url, contentType string, body io.Reader) (*Response, error)
- func (s *Session) PostForm(url string, data url.Values) (*Response, error)
- func (s *Session) PostFormWithContext(ctx context.Context, url string, data url.Values) (*Response, error)
- func (s *Session) PostWithContext(ctx context.Context, url, contentType string, body io.Reader) (*Response, error)
- func (s *Session) Put(url, contentType string, body io.Reader) (*Response, error)
- func (s *Session) PutWithContext(ctx context.Context, url, contentType string, body io.Reader) (*Response, error)
- func (s *Session) SetKeepAlives(keepAlives bool) *Session
- func (s *Session) Timeout(timeout time.Duration) *Session
- func (s *Session) Upload(url, file string) (*Response, error)
- func (s *Session) Uploadmultipart(url, file string, fields map[string]string) (*Response, error)
- func (s *Session) WithOption(opts ...Option) *Session
- type Stat
- type WriteCounter
Constants ¶
This section is empty.
Variables ¶
var ( MethodGet = Method("GET") MethodPost = Method("POST") )
Method http method
Functions ¶
func DownloadFile ¶
DownloadFile download file
func DumpRequest ¶
DumpRequest returns the given request in its HTTP/1.x wire representation.
func DumpRequestIndent ¶
DumpRequestIndent warp Dump
func NewRequestWithContext ¶
NewRequestWithContext request
Types ¶
type Option ¶
type Option func(*Options)
Option func
func Proxy ¶
Proxy set proxy addr os.Setenv("HTTP_PROXY", "http://127.0.0.1:9743") os.Setenv("HTTPS_PROXY", "https://127.0.0.1:9743") https://stackoverflow.com/questions/14661511/setting-up-proxy-for-http-client
type Options ¶
type Options struct { Method string URL string Path []string Params map[string]any Header http.Header Cookies []http.Cookie Timeout time.Duration MaxConns int TraceLv int TraceLimit int Verify bool Logf func(ctx context.Context, stat Stat) Stream func(int64, []byte) error RequestEach []func(context.Context, *http.Request) error ResponseEach []func(context.Context, *http.Response) error // session used LocalAddr net.Addr Hosts map[string][]string // 内部host文件 Proxy func(*http.Request) (*url.URL, error) // contains filtered or unexported fields }
Options request
type Response ¶
type Response struct { *http.Response *http.Request // 这里是为了,保证存在请求发起失败的情况下,response=nil,request还能获取到原始记录 StartAt time.Time Cost time.Duration Retry int Err error // contains filtered or unexported fields }
Response wrap std response
func (*Response) JSON ¶
JSON parse response. Deprecated: DO NOT USE IT. Because it's not compatible with the standard library.
type Session ¶
Session httpclient session Clients and Transports are safe for concurrent use by multiple goroutines for efficiency should only be created once and re-used. so, session is also safe for concurrent use by multiple goroutines.
func (*Session) DebugTrace ¶
DebugTrace trace a request
func (*Session) DeleteWithContext ¶
func (s *Session) DeleteWithContext(ctx context.Context, url, contentType string, body io.Reader) (*Response, error)
DeleteWithContext send delete request
func (*Session) DoWithContext ¶
func (s *Session) DoWithContext(ctx context.Context, method, url, contentType string, body io.Reader) (*Response, error)
DoWithContext http request
func (*Session) GetWithContext ¶
GetWithContext http request
func (*Session) PostFormWithContext ¶
func (s *Session) PostFormWithContext(ctx context.Context, url string, data url.Values) (*Response, error)
PostFormWithContext post form request
func (*Session) PostWithContext ¶
func (s *Session) PostWithContext(ctx context.Context, url, contentType string, body io.Reader) (*Response, error)
PostWithContext send post request
func (*Session) PutWithContext ¶
func (s *Session) PutWithContext(ctx context.Context, url, contentType string, body io.Reader) (*Response, error)
PutWithContext send put request
func (*Session) SetKeepAlives ¶
SetKeepAlives set transport disableKeepAlives default transport is keepalive, if set false, only use the connection to the server for a single HTTP request.
func (*Session) Uploadmultipart ¶
Uploadmultipart upload with multipart form
func (*Session) WithOption ¶
type Stat ¶
type Stat struct { StartAt string `json:"StartAt"` Cost int64 `json:"Cost"` Request struct { Method string `json:"Method"` Header map[string]string `json:"Header"` URL string `json:"URL"` Body any `json:"Body"` } `json:"Request"` Response struct { Header map[string]string `json:"Header"` Body any `json:"Body"` StatusCode int `json:"StatusCode"` ContentLength int64 `json:"ContentLength"` } `json:"Response"` Err string `json:"Err"` //Exception string `json:"Exception"` Retry int `json:"Retry"` }
Stat stats
type WriteCounter ¶
WriteCounter WriteCounter
func (*WriteCounter) PrintProgress ¶
func (wc *WriteCounter) PrintProgress()
PrintProgress prints the progress of a file write