requests

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	GET     = "GET"
	POST    = "POST"
	PUT     = "PUT"
	DELETE  = "DELETE"
	OPTIONS = "OPTIONS"
	HEAD    = "HEAD"
	PATCH   = "PATCH"
)

Variables

View Source
var (
	// ErrHookFuncMaxLimit will be throwed when the number of hook functions
	// more than MaxLimit = 8
	ErrHookFuncMaxLimit = errors.New("HOOK函数数量要少于8个")

	// ErrIndexOutofBound means the index out of bound
	ErrIndexOutOfBound = errors.New("Index超出")
)

Functions

func MapToJsonString

func MapToJsonString(m J) (string, error)

func MapToQueryString

func MapToQueryString(m KV) string

Types

type AfterResponseHookFunction added in v1.0.0

type AfterResponseHookFunction func(r *Response) error

type BeforeRequestHookFunction added in v1.0.0

type BeforeRequestHookFunction func(*http.Request) error

type F

type F struct {
	Filename string
	Buffer   []byte
}

type Files

type Files map[string]F

文件类型结构体

type J

type J map[string]interface{}

type KV

type KV map[string]string

func (KV) Get

func (k KV) Get(name string) string

func (KV) Set

func (k KV) Set(name string, value string)

type L

type L []interface{}

列表结构体

type P

type P struct {
	Method            string
	Params            KV
	Data              KV
	DataString        string
	Json              J
	JsonString        string
	Headers           KV
	Cookies           KV
	Files             Files
	Form              KV
	Proxies           string
	NotAllowRedirects bool
	Timeout           time.Duration
	Retry             int
	MaxRetry          int
}

请求参数

type Response

type Response struct {
	Content        []byte
	StatusCode     int
	Header         http.Header
	Cookie         KV
	OriginResponse *http.Response
}

func Delete

func Delete(url string, params *P) (*Response, error)

func Get

func Get(url string, params *P) (*Response, error)
func Head(url string, params *P) (*Response, error)

func Options added in v1.0.0

func Options(url string, params *P) (*Response, error)

func Post

func Post(url string, params *P) (*Response, error)

func Put

func Put(url string, params *P) (*Response, error)

func Request

func Request(url string, params *P) (*Response, error)

func (*Response) IOReadCloser added in v1.0.1

func (r *Response) IOReadCloser() io.ReadCloser

response body to io.ReadCloser

func (*Response) Json

func (r *Response) Json(result any) error

To Struct response text like {"status": "1", "state": "success", "data": [{"value": 7396}, {"value": 5018}]}

type vv struct {
	Value int `json:"value"`
}

type rr struct {
	Status string `json:"status"`
	State  string `json:"state"`
	Data   []vv   `json:"data"`
}

var res rr _ := resp.Json(&res) fmt.Println(res)

To Map var res map[string]interface _ := resp.Json(&res) fmt.Println(res)

func (*Response) Text

func (r *Response) Text() string

type Session

type Session struct {
	Headers KV
	Cookies KV

	BaseUrl  string
	Timeout  time.Duration
	Proxies  string
	MaxRetry int
	// contains filtered or unexported fields
}

func NewSession

func NewSession() *Session

func (*Session) Delete

func (s *Session) Delete(url string, params *P) (*Response, error)

func (*Session) Get

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

func (*Session) Head added in v1.0.2

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

func (*Session) Options added in v1.0.2

func (s *Session) Options(url string, params *P) (*Response, error)

func (*Session) Patch added in v1.0.2

func (s *Session) Patch(url string, params *P) (*Response, error)

func (*Session) Post

func (s *Session) Post(url string, params *P) (*Response, error)

func (*Session) Put

func (s *Session) Put(url string, params *P) (*Response, error)

func (*Session) RegisterAfterResponseHook added in v1.0.2

func (s *Session) RegisterAfterResponseHook(fn AfterResponseHookFunction) (int, error)

注册响应后的HOOK函数,返回注册成功的 index

func (*Session) RegisterBeforeRequestHook added in v1.0.2

func (s *Session) RegisterBeforeRequestHook(fn BeforeRequestHookFunction) (int, error)

注册请求前的HOOK函数,返回注册成功的 index

func (*Session) Request

func (session *Session) Request(url string, p *P) (*Response, error)

func (*Session) ResetAfterResponseHook added in v1.0.2

func (s *Session) ResetAfterResponseHook()

重置响应后的HOOK函数

func (*Session) ResetBeforeRequestHook added in v1.0.2

func (s *Session) ResetBeforeRequestHook()

重置请求前的HOOK函数

func (*Session) UnregisterAfterResponseHook added in v1.0.2

func (s *Session) UnregisterAfterResponseHook(index int) error

注销响应后的HOOK函数

func (*Session) UnregisterBeforeRequestHook added in v1.0.2

func (s *Session) UnregisterBeforeRequestHook(index int) error

注销请求前的HOOK函数

Jump to

Keyboard shortcuts

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