req

package module
v1.4.3 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2023 License: GPL-3.0 Imports: 9 Imported by: 0

README

req

A lightweight high-performance request library based on fasthttp

demo
// direct request
client := req.NewClient()
url := "https://baidu.com"
res, err := client.GET(url).Do()
if err != nil {
    return
}
fmt.Println(res.Body)

// request with query or body
client := req.NewClient()
cfg := &req.Config{
    Params: map[string]interface{}{
    "q1": 1,
    "q2": 2,
    },
    Data: map[string]interface{}{
    "data1": 1,
    "data2": 2,
    },
	Timeout: 10, // second
	GetHeaders: true,
}
url := "https://api.xxx.com"
res, err := client.POST(url, cfg).Do()
if err != nil {
    return
}
fmt.Println(res.Body)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultConfig = &GlobalConfig{
	IsLog: false,
	LogHandler: func(uri string, payload []byte, res Response, err error) {
		log.Println("status:", res.Status, "url:", uri, "payload:", string(payload), "resp:", res.String(), "err:", err)
	},
	NotOkError:  errors.New("request not success"),
	JSONEncoder: json.Marshal,
	JSONDecoder: json.Unmarshal,
}
View Source
var ErrTimeout = &TimeoutError{}

ErrTimeout is returned from timed out calls.

Functions

func GetQueryString

func GetQueryString(params map[string]interface{}) []byte

func IsNil added in v1.2.6

func IsNil(v interface{}) bool

func IsValueArray added in v1.2.7

func IsValueArray(v reflect.Value) bool

func IsValueNil added in v1.2.7

func IsValueNil(v reflect.Value) bool

func IsValuePtr added in v1.2.7

func IsValuePtr(v reflect.Value) bool

func IsValueSlice added in v1.2.7

func IsValueSlice(v reflect.Value) bool

func Log

func Log(uri string, payload []byte, res *Response, err error)

Log uri:请求的完整路径,payload:请求负载,res:响应,err:请求错误,ErrNoFreeConns is returned if all DefaultMaxConnsPerHost connections to the requested host are busy.

Types

type Args added in v1.2.1

type Args = fasthttp.Args

type BasicAuth

type BasicAuth struct {
	Username, Password string
}

var client = &fasthttp.Client{}

type Body added in v1.1.0

type Body []byte

func (Body) Map added in v1.1.0

func (body Body) Map() (map[string]interface{}, error)

func (Body) String added in v1.1.0

func (body Body) String() string

func (Body) Struct added in v1.1.0

func (body Body) Struct(s interface{}) error

type Client

type Client struct {
	*fasthttp.Client
	ClientConfig *ClientConfig
}

func NewClient

func NewClient(configs ...*ClientConfig) *Client

func (*Client) DELETE added in v1.4.2

func (c *Client) DELETE(url string, config ...*Config) (r *Req)

func (*Client) GET

func (c *Client) GET(url string, config ...*Config) (req *Req)

func (*Client) NewReq

func (c *Client) NewReq(url string, method string, config *Config) *Req

func (*Client) PATCH

func (c *Client) PATCH(url string, config *Config) (r *Req)

func (*Client) POST

func (c *Client) POST(url string, config *Config) (r *Req)

func (*Client) PUT added in v1.1.0

func (c *Client) PUT(url string, config *Config) (r *Req)

func (*Client) SetBasicAuth added in v1.4.1

func (c *Client) SetBasicAuth(Username, Password string)

func (*Client) SetBasicUrl added in v1.4.1

func (c *Client) SetBasicUrl(url string)

func (*Client) SetHeaders added in v1.4.1

func (c *Client) SetHeaders(headers map[string]string)

func (*Client) SetTimeout added in v1.4.1

func (c *Client) SetTimeout(timeout time.Duration)

type ClientConfig added in v1.4.1

type ClientConfig struct {
	BasicAuth *BasicAuth
	Headers   map[string]string
	BasicUrl  string
	Timeout   time.Duration //second
}

type Config

type Config struct {
	Params      map[string]interface{}
	Form        map[string]interface{}
	Data        interface{}
	Json        json.RawMessage
	Headers     map[string]string
	Timeout     time.Duration //second
	MaxRedirect int
	GetHeaders  bool
	BodyWriteTo io.Writer
}

type GlobalConfig

type GlobalConfig struct {
	IsLog       bool
	LogHandler  func(uri string, payload []byte, res Response, err error)
	NotOkError  error
	JSONEncoder func(v interface{}) ([]byte, error)
	JSONDecoder func(data []byte, v interface{}) error
}

type Req added in v1.4.0

type Req struct {
	*fasthttp.Request
	// contains filtered or unexported fields
}

func (*Req) AddForm added in v1.4.0

func (r *Req) AddForm(k string, v interface{}) *Req

func (*Req) AddForms added in v1.4.1

func (r *Req) AddForms(form map[string]interface{}) *Req

func (*Req) AddHeader added in v1.4.0

func (r *Req) AddHeader(k, v string) *Req

func (*Req) AddHeaders added in v1.4.1

func (r *Req) AddHeaders(headers map[string]string) *Req

func (*Req) AddParam added in v1.4.0

func (r *Req) AddParam(k string, v interface{}) *Req

func (*Req) AddParams added in v1.4.1

func (r *Req) AddParams(params map[string]interface{}) *Req

func (*Req) Do added in v1.4.0

func (r *Req) Do() (res *Response, err error)

func (*Req) SetHeader added in v1.4.1

func (r *Req) SetHeader(k, v string) *Req

func (*Req) SetHeaders added in v1.4.1

func (r *Req) SetHeaders(headers map[string]string) *Req

type Reqs

type Reqs interface {
	POST() interface{}
	GET() interface{}
}

type Response

type Response struct {
	Status int
	Body
	Header *fasthttp.ResponseHeader
}

func (*Response) Error

func (res *Response) Error(msg ...string) (err error)

func (*Response) IsOk

func (res *Response) IsOk() bool

type TimeoutError added in v1.4.3

type TimeoutError struct{}

func (*TimeoutError) Error added in v1.4.3

func (e *TimeoutError) Error() string

Jump to

Keyboard shortcuts

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