fastreq

package module
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2023 License: MIT Imports: 25 Imported by: 0

README

fastreq

Fast, convenient and simple HTTP client based on fasthttp for Go (inspired by Fiber and fasthttp)

Features

  • Extreme performance
  • Low memory footprint
  • Middleware support

Installation

go get github.com/wnanbei/fastreq

import "github.com/wnanbei/fastreq"

Usage

import "github.com/wnanbei/fastreq"

resp, err := fastreq.Get("https://hello-world", fastreq.NewArgs())
if err != nil {
    panic(err)
}
fmt.Println(resp.BodyString())

BenchMark

Documentation

Index

Constants

View Source
const (
	MIMETextXML                          = "text/xml"
	MIMETextHTML                         = "text/html"
	MIMETextPlain                        = "text/plain"
	MIMEApplicationXML                   = "application/xml"
	MIMEApplicationJSON                  = "application/json"
	MIMEApplicationJavaScript            = "application/javascript"
	MIMEApplicationForm                  = "application/x-www-form-urlencoded"
	MIMEOctetStream                      = "application/octet-stream"
	MIMEMultipartForm                    = "multipart/form-data"
	MIMETextXMLCharsetUTF8               = "text/xml; charset=utf-8"
	MIMETextHTMLCharsetUTF8              = "text/html; charset=utf-8"
	MIMETextPlainCharsetUTF8             = "text/plain; charset=utf-8"
	MIMEApplicationXMLCharsetUTF8        = "application/xml; charset=utf-8"
	MIMEApplicationJSONCharsetUTF8       = "application/json; charset=utf-8"
	MIMEApplicationJavaScriptCharsetUTF8 = "application/javascript; charset=utf-8"
)
View Source
const Version = "0.3.2"

Version of fastreq

Variables

This section is empty.

Functions

func DownloadFile added in v0.2.0

func DownloadFile(req *Request, path, filename string) error

func Release

func Release(releasers ...Releaser)

func SetDebugLevel added in v0.2.2

func SetDebugLevel(lvl DebugLevel)

func SetDefaultClient added in v0.3.1

func SetDefaultClient(client *Client)

func SetEnvHTTPProxy

func SetEnvHTTPProxy()

func SetHTTPProxy

func SetHTTPProxy(proxy string)

func SetJsonMarshal added in v0.2.2

func SetJsonMarshal(f func(any) ([]byte, error))

SetJsonMarshal can set json marshal function. Default Marshal is github.com/json-iterator/go

func SetJsonUnmarshal added in v0.2.2

func SetJsonUnmarshal(f func([]byte, any) error)

SetJsonUnmarshal can set json unmarshal function. Default Unmarshal is github.com/json-iterator/go

func SetOauth1

func SetOauth1(o *Oauth1)

func SetSocks5Proxy

func SetSocks5Proxy(proxy string)

Types

type Body added in v0.3.0

type Body struct {
	// contains filtered or unexported fields
}

func NewBody added in v0.3.0

func NewBody(b []byte) *Body

func (*Body) AutoRelease added in v0.3.2

func (b *Body) AutoRelease(auto bool)

func (*Body) BindRequest added in v0.3.0

func (b *Body) BindRequest(req *Request) error

func (*Body) Release added in v0.3.2

func (b *Body) Release()

type Client

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

Client ...

func NewClient

func NewClient() *Client

NewClient ...

func NewClientFromFastHTTP

func NewClientFromFastHTTP(client *fasthttp.Client) *Client

NewClientFromFastHTTP ...

func (*Client) AddMiddleware added in v0.2.1

func (c *Client) AddMiddleware(middlewares ...Middleware)

func (*Client) Connect added in v0.2.2

func (c *Client) Connect(url string, opts ...ReqOption) (*Response, error)

func (*Client) Delete

func (c *Client) Delete(url string, opts ...ReqOption) (*Response, error)

func (*Client) Do

func (c *Client) Do(req *Request, opts ...ReqOption) (*Response, error)

func (*Client) DownloadFile added in v0.2.0

func (c *Client) DownloadFile(req *Request, path, filename string) error

func (*Client) Get

func (c *Client) Get(url string, opts ...ReqOption) (*Response, error)

func (*Client) Head

func (c *Client) Head(url string, opts ...ReqOption) (*Response, error)

func (*Client) Options added in v0.3.1

func (c *Client) Options(url string, opts ...ReqOption) (*Response, error)

func (*Client) Patch

func (c *Client) Patch(url string, opts ...ReqOption) (*Response, error)

func (*Client) Post

func (c *Client) Post(url string, opts ...ReqOption) (*Response, error)

func (*Client) Put

func (c *Client) Put(url string, opts ...ReqOption) (*Response, error)

func (*Client) SetDebugLevel added in v0.2.2

func (c *Client) SetDebugLevel(lvl DebugLevel)

func (*Client) SetDefaultUserAgent added in v0.3.2

func (c *Client) SetDefaultUserAgent(userAgent string)

func (*Client) SetEnvHTTPProxy

func (c *Client) SetEnvHTTPProxy()

func (*Client) SetHTTPProxy

func (c *Client) SetHTTPProxy(proxy string)

func (*Client) SetMaxRedirectsCount

func (c *Client) SetMaxRedirectsCount(count int)

func (*Client) SetOauth1

func (c *Client) SetOauth1(o *Oauth1)

func (*Client) SetRetryIf

func (c *Client) SetRetryIf(retryIf fasthttp.RetryIfFunc)

func (*Client) SetSocks5Proxy

func (c *Client) SetSocks5Proxy(proxy string)

func (*Client) SetTLSConfig

func (c *Client) SetTLSConfig(config *tls.Config)

func (*Client) SetTimeout

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

func (*Client) SkipInsecureVerify

func (c *Client) SkipInsecureVerify(isSkip bool)

type Cookies added in v0.3.2

type Cookies struct {
	// contains filtered or unexported fields
}

func NewCookies added in v0.3.2

func NewCookies(kv ...string) *Cookies

func (*Cookies) AutoRelease added in v0.3.2

func (c *Cookies) AutoRelease(auto bool)

func (*Cookies) BindRequest added in v0.3.2

func (c *Cookies) BindRequest(req *Request) error

func (*Cookies) Release added in v0.3.2

func (c *Cookies) Release()

type Ctx added in v0.2.0

type Ctx struct {
	Request  *Request
	Response *Response
	// contains filtered or unexported fields
}

Ctx represents the Context which hold the HTTP request and response.

func NewCtx added in v0.3.2

func NewCtx() *Ctx

func (*Ctx) Next added in v0.2.0

func (c *Ctx) Next() (err error)

Next ..

func (*Ctx) Release added in v0.2.1

func (c *Ctx) Release()

type DebugLevel added in v0.2.2

type DebugLevel int

DebugLevel debug log level

const (
	DebugClose DebugLevel = iota // close debug
	DebugSimple
	DebugDetail
)

type HTTPMethod

type HTTPMethod string

HTTPMethod http request method

const (
	GET     HTTPMethod = "GET"     // RFC 7231, 4.3.1
	HEAD    HTTPMethod = "HEAD"    // RFC 7231, 4.3.2
	POST    HTTPMethod = "POST"    // RFC 7231, 4.3.3
	PUT     HTTPMethod = "PUT"     // RFC 7231, 4.3.4
	PATCH   HTTPMethod = "PATCH"   // RFC 5789
	DELETE  HTTPMethod = "DELETE"  // RFC 7231, 4.3.5
	CONNECT HTTPMethod = "CONNECT" // RFC 7231, 4.3.6
	OPTIONS HTTPMethod = "OPTIONS" // RFC 7231, 4.3.7
)

type Headers added in v0.3.2

type Headers struct {
	// contains filtered or unexported fields
}

func NewHeaders added in v0.3.2

func NewHeaders(kv ...string) *Headers

func (*Headers) Add added in v0.3.2

func (h *Headers) Add(key, value string)

Add adds the given 'key: value' header.

Multiple headers with the same key may be added with this function. Use Set for setting a single header for the given key.

func (*Headers) AddBytesK added in v0.3.2

func (h *Headers) AddBytesK(key []byte, value string)

AddBytesK adds the given 'key: value' header.

Multiple headers with the same key may be added with this function. Use SetBytesK for setting a single header for the given key.

func (*Headers) AddBytesKV added in v0.3.2

func (h *Headers) AddBytesKV(key, value []byte)

AddBytesKV adds the given 'key: value' header.

Multiple headers with the same key may be added with this function. Use SetBytesKV for setting a single header for the given key.

the Content-Type, Content-Length, Connection, Cookie, Transfer-Encoding, Host and User-Agent headers can only be set once and will overwrite the previous value.

func (*Headers) AddBytesV added in v0.3.2

func (h *Headers) AddBytesV(key string, value []byte)

AddBytesV adds the given 'key: value' header.

Multiple headers with the same key may be added with this function. Use SetBytesV for setting a single header for the given key.

func (*Headers) AutoRelease added in v0.3.2

func (h *Headers) AutoRelease(auto bool)

func (*Headers) BindRequest added in v0.3.2

func (h *Headers) BindRequest(req *Request) error

func (*Headers) Del added in v0.3.2

func (h *Headers) Del(key string)

Del deletes header with the given key.

func (*Headers) DelBytes added in v0.3.2

func (h *Headers) DelBytes(key []byte)

DelBytes deletes header with the given key.

func (*Headers) Peek added in v0.3.2

func (h *Headers) Peek(key string) []byte

Peek returns header value for the given key.

The returned value is valid until the request is released, either though ReleaseRequest or your request handler returning. Do not store references to returned value. Make copies instead.

func (*Headers) PeekAll added in v0.3.2

func (h *Headers) PeekAll(key string) [][]byte

PeekAll returns all header value for the given key.

The returned value is valid until the request is released, either though ReleaseRequest or your request handler returning. Any future calls to the Peek* will modify the returned value. Do not store references to returned value. Make copies instead.

func (*Headers) PeekBytes added in v0.3.2

func (h *Headers) PeekBytes(key []byte) []byte

PeekBytes returns header value for the given key.

The returned value is valid until the request is released, either though ReleaseRequest or your request handler returning. Do not store references to returned value. Make copies instead.

func (*Headers) Release added in v0.3.2

func (h *Headers) Release()

func (*Headers) Set added in v0.3.2

func (h *Headers) Set(key, value string)

Set sets the given 'key: value' header.

Use Add for setting multiple header values under the same key.

func (*Headers) SetBytesK added in v0.3.2

func (h *Headers) SetBytesK(key []byte, value string)

SetBytesK sets the given 'key: value' header.

Use AddBytesK for setting multiple header values under the same key.

func (*Headers) SetBytesKV added in v0.3.2

func (h *Headers) SetBytesKV(key, value []byte)

SetBytesKV sets the given 'key: value' header.

Use AddBytesKV for setting multiple header values under the same key.

func (*Headers) SetBytesV added in v0.3.2

func (h *Headers) SetBytesV(key string, value []byte)

SetBytesV sets the given 'key: value' header.

Use AddBytesV for setting multiple header values under the same key.

func (*Headers) VisitAll added in v0.3.2

func (h *Headers) VisitAll(f func(key, value []byte))

VisitAll calls f for each header.

f must not retain references to key and/or value after returning. Copy key and/or value contents before returning if you need retaining them.

To get the headers in order they were received use VisitAllInOrder.

type JsonBody added in v0.3.0

type JsonBody struct {
	// contains filtered or unexported fields
}

func NewJsonBody added in v0.3.0

func NewJsonBody(b interface{}) *JsonBody

func (*JsonBody) AutoRelease added in v0.3.2

func (b *JsonBody) AutoRelease(auto bool)

func (*JsonBody) BindRequest added in v0.3.0

func (b *JsonBody) BindRequest(req *Request) error

func (*JsonBody) Release added in v0.3.2

func (b *JsonBody) Release()

type Middleware added in v0.2.0

type Middleware func(ctx *Ctx) error

func MiddlewareOauth1

func MiddlewareOauth1(o *Oauth1) Middleware

type MultipartForm added in v0.3.1

type MultipartForm struct {
	*fasthttp.Args
	Boundary string
	// contains filtered or unexported fields
}

func NewMultipartForm added in v0.3.1

func NewMultipartForm(boundary string, kv ...string) *MultipartForm

func (*MultipartForm) AutoRelease added in v0.3.2

func (mf *MultipartForm) AutoRelease(auto bool)

func (*MultipartForm) BindRequest added in v0.3.1

func (mf *MultipartForm) BindRequest(req *Request) error

func (*MultipartForm) Release added in v0.3.1

func (mf *MultipartForm) Release()

type Oauth1

type Oauth1 struct {
	ConsumerKey    string
	ConsumerSecret string
	AccessToken    string
	AccessSecret   string
}

func (Oauth1) GenHeader

func (o Oauth1) GenHeader(req *Request) []byte

type PostForm added in v0.3.0

type PostForm struct {
	*fasthttp.Args
	// contains filtered or unexported fields
}

func NewPostForm added in v0.3.0

func NewPostForm(kv ...string) *PostForm

func (*PostForm) AutoRelease added in v0.3.2

func (f *PostForm) AutoRelease(auto bool)

func (*PostForm) BindRequest added in v0.3.0

func (f *PostForm) BindRequest(req *Request) error

func (*PostForm) Release added in v0.3.0

func (f *PostForm) Release()

type QueryParams added in v0.3.0

type QueryParams struct {
	*fasthttp.Args
	// contains filtered or unexported fields
}

func NewQueryParams added in v0.3.0

func NewQueryParams(kv ...string) *QueryParams

func (*QueryParams) AutoRelease added in v0.3.2

func (q *QueryParams) AutoRelease(auto bool)

func (*QueryParams) BindRequest added in v0.3.0

func (q *QueryParams) BindRequest(req *Request) error

func (*QueryParams) Release added in v0.3.0

func (q *QueryParams) Release()

type Releaser

type Releaser interface {
	Release()
}

type ReqOption added in v0.3.0

type ReqOption interface {
	Releaser
	BindRequest(req *Request) error
	AutoRelease(bool)
	// contains filtered or unexported methods
}

type Request

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

Request ...

func NewRequest

func NewRequest(method HTTPMethod, url string) *Request

func NewRequestFromFastHTTP

func NewRequestFromFastHTTP(req *fasthttp.Request) *Request

func (*Request) AddHeader

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

func (*Request) AddMFField added in v0.3.1

func (r *Request) AddMFField(field, value string) error

func (*Request) AddMFFile added in v0.3.1

func (r *Request) AddMFFile(fieldName, filePath string) error

func (*Request) Copy

func (r *Request) Copy() *Request

func (*Request) Release

func (r *Request) Release()

func (*Request) SetBasicAuth

func (r *Request) SetBasicAuth(username, password string)

func (*Request) SetBoundary added in v0.3.1

func (r *Request) SetBoundary(boundary string) error

func (*Request) SetContentType

func (r *Request) SetContentType(contentType string)

func (*Request) SetCookie

func (r *Request) SetCookie(key, value string)

func (*Request) SetCookies

func (r *Request) SetCookies(kv ...string)

func (*Request) SetHeader

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

func (*Request) SetHeaders

func (r *Request) SetHeaders(kv ...string)

func (*Request) SetHost

func (r *Request) SetHost(host string)

func (*Request) SetJSON added in v0.3.1

func (r *Request) SetJSON(v interface{}) error

func (*Request) SetMethod

func (r *Request) SetMethod(method HTTPMethod)

func (*Request) SetPostForm added in v0.3.1

func (r *Request) SetPostForm(form *PostForm)

func (*Request) SetQueryParams added in v0.2.0

func (r *Request) SetQueryParams(params *QueryParams)

func (*Request) SetQueryString

func (r *Request) SetQueryString(queryString string)

func (*Request) SetReferer

func (r *Request) SetReferer(referer string)

func (*Request) SetUserAgent

func (r *Request) SetUserAgent(userAgent string)

func (*Request) SetXML added in v0.3.1

func (r *Request) SetXML(v interface{}) error

type Response

type Response struct {
	*fasthttp.Response
	Request *fasthttp.Request
	Dom     *goquery.Document
}

Response ...

func Connect added in v0.2.2

func Connect(url string, opts ...ReqOption) (*Response, error)

func Delete

func Delete(url string, opts ...ReqOption) (*Response, error)

func Do

func Do(req *Request, opts ...ReqOption) (*Response, error)

func Get

func Get(url string, opts ...ReqOption) (*Response, error)
func Head(url string, opts ...ReqOption) (*Response, error)

func NewResponse

func NewResponse() *Response

func Options added in v0.3.1

func Options(url string, opts ...ReqOption) (*Response, error)

func Patch

func Patch(url string, opts ...ReqOption) (*Response, error)

func Post

func Post(url string, opts ...ReqOption) (*Response, error)

func Put

func Put(url string, opts ...ReqOption) (*Response, error)

func (*Response) BodyString

func (r *Response) BodyString() string

func (*Response) BuildDom added in v0.3.2

func (r *Response) BuildDom() error

func (*Response) Copy

func (r *Response) Copy() *Response

func (*Response) FileName added in v0.2.0

func (r *Response) FileName() string

func (*Response) Json

func (r *Response) Json(v interface{}) error

func (*Response) JsonGet

func (r *Response) JsonGet(path string) gjson.Result

func (*Response) JsonGetMany

func (r *Response) JsonGetMany(path ...string) []gjson.Result

func (*Response) JsonGetPartOf added in v0.2.2

func (r *Response) JsonGetPartOf(path string, v interface{}) error

func (*Response) Release

func (r *Response) Release()

func (*Response) SaveToFile added in v0.2.0

func (r *Response) SaveToFile(path, filename string) error

type Timeout added in v0.3.0

type Timeout struct {
	// contains filtered or unexported fields
}

func NewTimeout added in v0.3.2

func NewTimeout(t time.Duration) *Timeout

func (*Timeout) AutoRelease added in v0.3.2

func (t *Timeout) AutoRelease(auto bool)

func (*Timeout) BindRequest added in v0.3.0

func (t *Timeout) BindRequest(req *Request) error

func (*Timeout) Release added in v0.3.2

func (t *Timeout) Release()

Jump to

Keyboard shortcuts

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