requests

package module
v0.0.0-...-62b2025 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2024 License: Apache-2.0 Imports: 21 Imported by: 0

README

requests

deprecated and Archived

please using https://github.com/golang-io/requests.git

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	MethodGet  = Method("GET")
	MethodPost = Method("POST")
)

Method http method

Functions

func DownloadFile

func DownloadFile(url string, progress bool, filepath ...string) error

DownloadFile download file

func DumpRequest

func DumpRequest(req *http.Request) ([]byte, error)

DumpRequest returns the given request in its HTTP/1.x wire representation.

func DumpRequestIndent

func DumpRequestIndent(req *http.Request) string

DumpRequestIndent warp Dump

func Log

func Log(format string, v ...any)

func NewRequestWithContext

func NewRequestWithContext(ctx context.Context, options Options) (*http.Request, error)

NewRequestWithContext request

func Upload

func Upload(url, field, file string) (*http.Response, error)

Upload HttpUpload

func Wget

func Wget(url, name string) (int, error)

Wget download a file from remote.

Types

type Option

type Option func(*Options)

Option func

func BasicAuth

func BasicAuth(user, pass string) Option

BasicAuth base auth

func Body

func Body(body any) Option

Body request body

func Cookie(cookie http.Cookie) Option

Cookie cookie

func Cookies

func Cookies(cookies ...http.Cookie) Option

Cookies cookies

func Form

func Form(form url.Values) Option

Form set form, content-type is

func Gzip

func Gzip(body any) Option

Gzip request gzip compressed

func Header(k, v string) Option

Header header

func Headers

func Headers(kv map[string]string) Option

Headers headers

func Hosts

func Hosts(hosts map[string][]string) Option

Hosts 自定义Host配置,参数只能在session级别生效,格式:<host:port> 如果存在proxy服务,只能解析代理服务,不能解析url地址

func LocalAddr

func LocalAddr(addr net.Addr) Option

func Logf

func Logf(f func(context.Context, Stat)) Option

func MaxConns

func MaxConns(conn int) Option

MaxConns set max connections

func Method

func Method(method string) Option

Method set method

func Param

func Param(k string, v any) Option

Param params

func Params

func Params(query map[string]any) Option

Params add query args

func Path

func Path(path string) Option

Path set path

func Proxy

func Proxy(addr string) Option

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

func RequestEach

func RequestEach(each ...func(context.Context, *http.Request) error) Option

func ResponseEach

func ResponseEach(each ...func(context.Context, *http.Response) error) Option

func Stream

func Stream(stream func(int64, []byte) error) Option

func Timeout

func Timeout(timeout time.Duration) Option

Timeout client timeout duration

func TraceLv

func TraceLv(v int, limit ...int) Option

TraceLv Trace

func URL

func URL(url string) Option

URL set url

func Verify

func Verify(verify bool) Option

Verify verify

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

func (Options) Copy

func (opt Options) Copy() Options

Copy options

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 Delete

func Delete(url, contentType string, body io.Reader) (*Response, error)

Delete send delete request

func Get

func Get(url string) (*Response, error)

Get send get request

func Head(url string) (resp *Response, err error)

Head send post request

func PUT

func PUT(url, contentType string, body io.Reader) (*Response, error)

PUT send put request

func Post

func Post(url string, contentType string, body io.Reader) (*Response, error)

Post send post request

func PostForm

func PostForm(url string, data url.Values) (*Response, error)

PostForm send post request, content-type = application/x-www-form-urlencoded

func (*Response) Bytes

func (resp *Response) Bytes() []byte

Bytes to bytes

func (*Response) Download

func (resp *Response) Download(name string) (int, error)

Download parse response to a file

func (*Response) Dump

func (resp *Response) Dump() ([]byte, error)

Dump returns the given request in its HTTP/1.x wire representation.

func (*Response) Error

func (resp *Response) Error() string

func (*Response) JSON

func (resp *Response) JSON(v any) error

JSON parse response. Deprecated: DO NOT USE IT. Because it's not compatible with the standard library.

func (*Response) Stat

func (resp *Response) Stat() Stat

Stat stat

func (*Response) StdLib

func (resp *Response) StdLib() *http.Response

StdLib return net/http.Response

func (*Response) String

func (resp *Response) String() string

func (*Response) Text

func (resp *Response) Text() string

Text parse to string

type Session

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

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 New

func New(opts ...Option) *Session

New session

func (*Session) DebugTrace

func (s *Session) DebugTrace(req *http.Request, v int, limit int) (*http.Response, error)

DebugTrace trace a request

func (*Session) Delete

func (s *Session) Delete(url, contentType string, body io.Reader) (resp *Response, err error)

Delete send delete 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) Do

func (s *Session) Do(method, url, contentType string, body io.Reader) (*Response, error)

Do http request

func (*Session) DoRequest

func (s *Session) DoRequest(ctx context.Context, opts ...Option) (*Response, error)

DoRequest send a request and return a response

func (*Session) DoWithContext

func (s *Session) DoWithContext(ctx context.Context, method, url, contentType string, body io.Reader) (*Response, error)

DoWithContext http request

func (*Session) Get

func (s *Session) Get(url string) (*Response, error)

Get send get request

func (*Session) GetWithContext

func (s *Session) GetWithContext(ctx context.Context, url string) (*Response, error)

GetWithContext http request

func (*Session) Head

func (s *Session) Head(url string) (*Response, error)

Head send head request

func (*Session) Init

func (s *Session) Init(opts ...Option)

Init init

func (*Session) Post

func (s *Session) Post(url, contentType string, body io.Reader) (*Response, error)

Post send post request

func (*Session) PostForm

func (s *Session) PostForm(url string, data url.Values) (*Response, error)

PostForm post form 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) Put

func (s *Session) Put(url, contentType string, body io.Reader) (*Response, error)

Put send put 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

func (s *Session) SetKeepAlives(keepAlives bool) *Session

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) Timeout

func (s *Session) Timeout(timeout time.Duration) *Session

Timeout set client timeout

func (*Session) Upload

func (s *Session) Upload(url, file string) (*Response, error)

Upload upload file

func (*Session) Uploadmultipart

func (s *Session) Uploadmultipart(url, file string, fields map[string]string) (*Response, error)

Uploadmultipart upload with multipart form

func (*Session) WithOption

func (s *Session) WithOption(opts ...Option) *Session

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

func (Stat) String

func (stat Stat) String() string

type WriteCounter

type WriteCounter struct {
	Max   uint64
	Total uint64
}

WriteCounter WriteCounter

func (*WriteCounter) PrintProgress

func (wc *WriteCounter) PrintProgress()

PrintProgress prints the progress of a file write

func (*WriteCounter) Write

func (wc *WriteCounter) Write(p []byte) (int, error)

Write xx

Directories

Path Synopsis
cmd
proxy-httpd Module

Jump to

Keyboard shortcuts

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