client

package
v3.0.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2024 License: MIT Imports: 28 Imported by: 1

README

Fiber Client

Easy-to-use HTTP client based on fasthttp (inspired by resty and axios)

Features section describes in detail about Resty capabilities

Features

The characteristics have not yet been written.

  • GET, POST, PUT, DELETE, HEAD, PATCH, OPTIONS, etc.
  • Simple and chainable methods for settings and request
  • Request Body can be string, []byte, map, slice
    • Auto detects Content-Type
    • Buffer processing for files
    • Native *fasthttp.Request instance can be accessed during middleware and request execution via Request.RawRequest
    • Request Body can be read multiple time via Request.RawRequest.GetBody()
  • Response object gives you more possibility
    • Access as []byte by response.Body() or access as string by response.String()
  • Automatic marshal and unmarshal for JSON and XML content type
    • Default is JSON, if you supply struct/map without header Content-Type
    • For auto-unmarshal, refer to -
      • Success scenario Request.SetResult() and Response.Result().
      • Error scenario Request.SetError() and Response.Error().
      • Supports RFC7807 - application/problem+json & application/problem+xml
    • Provide an option to override JSON Marshal/Unmarshal and XML Marshal/Unmarshal

Usage

The following samples will assist you to become as comfortable as possible with Fiber Client library.

// Import Fiber Client into your code and refer it as `client`.
import "github.com/gofiber/fiber/client"

Simple GET

Documentation

Overview

The code has been taken from https://github.com/valyala/fasthttp/pull/526 originally.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidProxyURL    = errors.New("invalid proxy url scheme")
	ErrFailedToAppendCert = errors.New("failed to append certificate")
)
View Source
var (
	ErrTimeoutOrCancel      = errors.New("timeout or cancel")
	ErrURLFormat            = errors.New("the url is a mistake")
	ErrNotSupportSchema     = errors.New("the protocol is not support, only http or https")
	ErrFileNoName           = errors.New("the file should have name")
	ErrBodyType             = errors.New("the body type should be []byte")
	ErrNotSupportSaveMethod = errors.New("file path and io.Writer are supported")
)
View Source
var ErrClientNil = errors.New("client can not be nil")

Functions

func ReleaseCookieJar

func ReleaseCookieJar(c *CookieJar)

ReleaseCookieJar returns CookieJar to the pool.

func ReleaseFile

func ReleaseFile(f *File)

ReleaseFile returns the object acquired via AcquireFile to the pool.

Do not access the released File object, otherwise data races may occur.

func ReleaseRequest

func ReleaseRequest(req *Request)

ReleaseRequest returns the object acquired via AcquireRequest to the pool.

Do not access the released Request object, otherwise data races may occur.

func ReleaseResponse

func ReleaseResponse(resp *Response)

ReleaseResponse returns the object acquired via AcquireResponse to the pool.

Do not access the released Response object, otherwise data races may occur.

func Replace

func Replace(c *Client) func()

Replace the defaultClient, the returned function can undo.

func SetValWithStruct

func SetValWithStruct(p WithStruct, tagName string, v any)

SetValWithStruct Set some values using structs. `p` is a structure that implements the WithStruct interface, The field name can be specified by `tagName`. `v` is a struct include some data. Note: This method only supports simple types and nested structs are not currently supported.

Types

type Client

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

The Client is used to create a Fiber Client with client-level settings that apply to all requests raise from the client.

Fiber Client also provides an option to override or merge most of the client settings at the request.

func C

func C() *Client

C get default client.

func New

func New() *Client

New creates and returns a new Client object.

func (*Client) AddHeader

func (c *Client) AddHeader(key, val string) *Client

AddHeader method adds a single header field and its value in the client instance. These headers will be applied to all requests raised from this client instance. Also, it can be overridden at request level header options.

func (*Client) AddHeaders

func (c *Client) AddHeaders(h map[string][]string) *Client

AddHeaders method adds multiple headers field and its values at one go in the client instance. These headers will be applied to all requests raised from this client instance. Also it can be overridden at request level headers options.

func (*Client) AddParam

func (c *Client) AddParam(key, val string) *Client

AddParam method adds a single query param field and its value in the client instance. These params will be applied to all requests raised from this client instance. Also, it can be overridden at request level param options.

func (*Client) AddParams

func (c *Client) AddParams(m map[string][]string) *Client

AddParams method adds multiple query params field and its values at one go in the client instance. These params will be applied to all requests raised from this client instance. Also it can be overridden at request level params options.

func (*Client) AddRequestHook

func (c *Client) AddRequestHook(h ...RequestHook) *Client

AddRequestHook Add user-defined request hooks.

func (*Client) AddResponseHook

func (c *Client) AddResponseHook(h ...ResponseHook) *Client

AddResponseHook Add user-defined response hooks.

func (*Client) BaseURL

func (c *Client) BaseURL() string

BaseURL returns baseurl in Client instance.

func (*Client) Cookie

func (c *Client) Cookie(key string) string

Cookie returns the cookie be set in request instance. if cookie doesn't exist, return empty string.

func (*Client) Custom

func (c *Client) Custom(url, method string, cfg ...Config) (*Response, error)

Custom provide an API like axios which send custom request.

func (*Client) Debug

func (c *Client) Debug() *Client

Debug enable log debug level output.

func (*Client) DelCookies

func (c *Client) DelCookies(key ...string) *Client

DelCookies method deletes single or multiple cookies field and its values in client.

func (*Client) DelParams

func (c *Client) DelParams(key ...string) *Client

DelParams method deletes single or multiple params field and its values in client.

func (*Client) DelPathParams

func (c *Client) DelPathParams(key ...string) *Client

DelPathParams method deletes single or multiple path params field and its values in client.

func (*Client) Delete

func (c *Client) Delete(url string, cfg ...Config) (*Response, error)

Delete provide an API like axios which send delete request.

func (*Client) DisableDebug

func (c *Client) DisableDebug() *Client

DisableDebug disenable log debug level output.

func (*Client) Get

func (c *Client) Get(url string, cfg ...Config) (*Response, error)

Get provide an API like axios which send get request.

func (*Client) Head

func (c *Client) Head(url string, cfg ...Config) (*Response, error)

Head provide a API like axios which send head request.

func (*Client) Header

func (c *Client) Header(key string) []string

Header method returns header value via key, this method will visit all field in the header, then sort them.

func (*Client) JSONMarshal

func (c *Client) JSONMarshal() utils.JSONMarshal

JSONMarshal returns json marshal function in Core.

func (*Client) JSONUnmarshal

func (c *Client) JSONUnmarshal() utils.JSONUnmarshal

JSONUnmarshal returns json unmarshal function in Core.

func (*Client) Logger

func (c *Client) Logger() log.CommonLogger

Logger returns logger instance of client.

func (*Client) Options

func (c *Client) Options(url string, cfg ...Config) (*Response, error)

Options provide an API like axios which send options request.

func (*Client) Param

func (c *Client) Param(key string) []string

Param method returns params value via key, this method will visit all field in the query param.

func (*Client) Patch

func (c *Client) Patch(url string, cfg ...Config) (*Response, error)

Patch provide an API like axios which send patch request.

func (*Client) PathParam

func (c *Client) PathParam(key string) string

PathParam returns the path param be set in request instance. if path param doesn't exist, return empty string.

func (*Client) Post

func (c *Client) Post(url string, cfg ...Config) (*Response, error)

Post provide an API like axios which send post request.

func (*Client) Put

func (c *Client) Put(url string, cfg ...Config) (*Response, error)

Put provide an API like axios which send put request.

func (*Client) R

func (c *Client) R() *Request

R raise a request from the client.

func (*Client) RequestHook

func (c *Client) RequestHook() []RequestHook

RequestHook Request returns user-defined request hooks.

func (*Client) Reset

func (c *Client) Reset()

Reset clear Client object

func (*Client) ResponseHook

func (c *Client) ResponseHook() []ResponseHook

ResponseHook return user-define response hooks.

func (*Client) RetryConfig

func (c *Client) RetryConfig() *RetryConfig

RetryConfig returns retry config in client.

func (*Client) SetBaseURL

func (c *Client) SetBaseURL(url string) *Client

SetBaseURL Set baseUrl which is prefix of real url.

func (*Client) SetCertificates

func (c *Client) SetCertificates(certs ...tls.Certificate) *Client

SetCertificates method sets client certificates into client.

func (*Client) SetCookie

func (c *Client) SetCookie(key, val string) *Client

SetCookie method sets a single cookie field and its value in the client instance. These cookies will be applied to all requests raised from this client instance. Also it can be overridden at request level cookie options.

func (*Client) SetCookieJar

func (c *Client) SetCookieJar(cookieJar *CookieJar) *Client

SetCookieJar sets cookie jar in client instance.

func (*Client) SetCookies

func (c *Client) SetCookies(m map[string]string) *Client

SetCookies method sets multiple cookies field and its values at one go in the client instance. These cookies will be applied to all requests raised from this client instance. Also it can be overridden at request level cookie options.

func (*Client) SetCookiesWithStruct

func (c *Client) SetCookiesWithStruct(v any) *Client

SetCookiesWithStruct method sets multiple cookies field and its values at one go in the client instance. These cookies will be applied to all requests raised from this client instance. Also it can be overridden at request level cookies options.

func (*Client) SetDial

func (c *Client) SetDial(dial fasthttp.DialFunc) *Client

SetDial sets dial function in client.

func (*Client) SetHeader

func (c *Client) SetHeader(key, val string) *Client

SetHeader method sets a single header field and its value in the client instance. These headers will be applied to all requests raised from this client instance. Also, it can be overridden at request level header options.

func (*Client) SetHeaders

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

SetHeaders method sets multiple headers field and its values at one go in the client instance. These headers will be applied to all requests raised from this client instance. Also it can be overridden at request level headers options.

func (*Client) SetJSONMarshal

func (c *Client) SetJSONMarshal(f utils.JSONMarshal) *Client

SetJSONMarshal Set json encoder.

func (*Client) SetJSONUnmarshal

func (c *Client) SetJSONUnmarshal(f utils.JSONUnmarshal) *Client

Set json decoder.

func (*Client) SetLogger

func (c *Client) SetLogger(logger log.CommonLogger) *Client

SetLogger sets logger instance in client.

func (*Client) SetParam

func (c *Client) SetParam(key, val string) *Client

SetParam method sets a single query param field and its value in the client instance. These params will be applied to all requests raised from this client instance. Also, it can be overridden at request level param options.

func (*Client) SetParams

func (c *Client) SetParams(m map[string]string) *Client

SetParams method sets multiple params field and its values at one go in the client instance. These params will be applied to all requests raised from this client instance. Also it can be overridden at request level params options.

func (*Client) SetParamsWithStruct

func (c *Client) SetParamsWithStruct(v any) *Client

SetParamsWithStruct method sets multiple params field and its values at one go in the client instance. These params will be applied to all requests raised from this client instance. Also it can be overridden at request level params options.

func (*Client) SetPathParam

func (c *Client) SetPathParam(key, val string) *Client

SetPathParam method sets a single path param field and its value in the client instance. These path params will be applied to all requests raised from this client instance. Also it can be overridden at request level path params options.

func (*Client) SetPathParams

func (c *Client) SetPathParams(m map[string]string) *Client

SetPathParams method sets multiple path params field and its values at one go in the client instance. These path params will be applied to all requests raised from this client instance. Also it can be overridden at request level path params options.

func (*Client) SetPathParamsWithStruct

func (c *Client) SetPathParamsWithStruct(v any) *Client

SetPathParamsWithStruct method sets multiple path params field and its values at one go in the client instance. These path params will be applied to all requests raised from this client instance. Also it can be overridden at request level path params options.

func (*Client) SetProxyURL

func (c *Client) SetProxyURL(proxyURL string) error

SetProxyURL sets proxy url in client. It will apply via core to hostclient.

func (*Client) SetReferer

func (c *Client) SetReferer(r string) *Client

SetReferer method sets referer field and its value in the client instance. This referer will be applied to all requests raised from this client instance. Also it can be overridden at request level referer options.

func (*Client) SetRetryConfig

func (c *Client) SetRetryConfig(config *RetryConfig) *Client

SetRetryConfig sets retry config in client which is impl by addon/retry package.

func (*Client) SetRootCertificate

func (c *Client) SetRootCertificate(path string) *Client

SetRootCertificate adds one or more root certificates into client.

func (*Client) SetRootCertificateFromString

func (c *Client) SetRootCertificateFromString(pem string) *Client

SetRootCertificateFromString method adds one or more root certificates into client.

func (*Client) SetTLSConfig

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

SetTLSConfig sets tlsConfig in client.

func (*Client) SetTimeout

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

SetTimeout method sets timeout val in client instance. This value will be applied to all requests raised from this client instance. Also, it can be overridden at request level timeout options.

func (*Client) SetUserAgent

func (c *Client) SetUserAgent(ua string) *Client

SetUserAgent method sets userAgent field and its value in the client instance. This ua will be applied to all requests raised from this client instance. Also it can be overridden at request level ua options.

func (*Client) SetXMLMarshal

func (c *Client) SetXMLMarshal(f utils.XMLMarshal) *Client

SetXMLMarshal Set xml encoder.

func (*Client) SetXMLUnmarshal

func (c *Client) SetXMLUnmarshal(f utils.XMLUnmarshal) *Client

SetXMLUnmarshal Set xml decoder.

func (*Client) TLSConfig

func (c *Client) TLSConfig() *tls.Config

TLSConfig returns tlsConfig in client. If client don't have tlsConfig, this function will init it.

func (*Client) XMLMarshal

func (c *Client) XMLMarshal() utils.XMLMarshal

XMLMarshal returns xml marshal function in Core.

func (*Client) XMLUnmarshal

func (c *Client) XMLUnmarshal() utils.XMLUnmarshal

XMLUnmarshal returns xml unmarshal function in Core.

type Config

type Config struct {
	Ctx context.Context //nolint:containedctx // It's needed to be stored in the config.

	UserAgent string
	Referer   string
	Header    map[string]string
	Param     map[string]string
	Cookie    map[string]string
	PathParam map[string]string

	Timeout      time.Duration
	MaxRedirects int

	Body     any
	FormData map[string]string
	File     []*File
}

Config for easy to set the request parameters, it should be noted that when setting the request body will use JSON as the default serialization mechanism, while the priority of Body is higher than FormData, and the priority of FormData is higher than File.

type Cookie map[string]string

Cookie is a map which to store the cookies.

func (Cookie) Add

func (c Cookie) Add(key, val string)

Add method impl the method in WithStruct interface.

func (Cookie) Del

func (c Cookie) Del(key string)

Del method impl the method in WithStruct interface.

func (Cookie) DelCookies

func (c Cookie) DelCookies(key ...string)

DelCookies method deletes multiple val in Cookie.

func (Cookie) Reset

func (c Cookie) Reset()

Reset clear the Cookie object.

func (Cookie) SetCookie

func (c Cookie) SetCookie(key, val string)

SetCookie method sets a single val in Cookie.

func (Cookie) SetCookies

func (c Cookie) SetCookies(m map[string]string)

SetCookies method sets multiple val in Cookie.

func (Cookie) SetCookiesWithStruct

func (c Cookie) SetCookiesWithStruct(v any)

SetCookiesWithStruct method sets multiple val in Cookie via a struct.

func (Cookie) VisitAll

func (c Cookie) VisitAll(f func(key, val string))

VisitAll method receive a function which can travel the all val.

type CookieJar

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

CookieJar manages cookie storage. It is used by the client to store cookies.

func AcquireCookieJar

func AcquireCookieJar() *CookieJar

AcquireCookieJar returns an empty CookieJar object from pool.

func (*CookieJar) Get

func (cj *CookieJar) Get(uri *fasthttp.URI) []*fasthttp.Cookie

Get returns the cookies stored from a specific domain. If there were no cookies related with host returned slice will be nil.

CookieJar keeps a copy of the cookies, so the returned cookies can be released safely.

func (*CookieJar) Release

func (cj *CookieJar) Release()

Release releases all cookie values.

func (*CookieJar) Set

func (cj *CookieJar) Set(uri *fasthttp.URI, cookies ...*fasthttp.Cookie)

Set sets cookies for a specific host. The host is get from uri.Host(). If the cookie key already exists it will be replaced by the new cookie value.

CookieJar keeps a copy of the cookies, so the parsed cookies can be released safely.

func (*CookieJar) SetByHost

func (cj *CookieJar) SetByHost(host []byte, cookies ...*fasthttp.Cookie)

SetByHost sets cookies for a specific host. If the cookie key already exists it will be replaced by the new cookie value.

CookieJar keeps a copy of the cookies, so the parsed cookies can be released safely.

func (*CookieJar) SetKeyValue

func (cj *CookieJar) SetKeyValue(host, key, value string)

SetKeyValue sets a cookie by key and value for a specific host.

This function prevents extra allocations by making repeated cookies not being duplicated.

func (*CookieJar) SetKeyValueBytes

func (cj *CookieJar) SetKeyValueBytes(host string, key, value []byte)

SetKeyValueBytes sets a cookie by key and value for a specific host.

This function prevents extra allocations by making repeated cookies not being duplicated.

type File

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

File is a struct which support send files via request.

func AcquireFile

func AcquireFile(setter ...SetFileFunc) *File

AcquireFile returns an File object from the pool. And you can set field in the File with SetFileFunc.

The returned file may be returned to the pool with ReleaseFile when no longer needed. This allows reducing GC load.

func (*File) Reset

func (f *File) Reset()

Reset clear the File object.

func (*File) SetFieldName

func (f *File) SetFieldName(n string)

SetFieldName method sets key of file in the body.

func (*File) SetName

func (f *File) SetName(n string)

SetName method sets file name.

func (*File) SetPath

func (f *File) SetPath(p string)

SetPath method set file path.

func (*File) SetReader

func (f *File) SetReader(r io.ReadCloser)

SetReader method can receive a io.ReadCloser which will be closed in parserBody hook.

type FormData

type FormData struct {
	*fasthttp.Args
}

FormData is a wrapper of fasthttp.Args, and it be used for url encode body and file body.

func (*FormData) AddData

func (f *FormData) AddData(key, val string)

AddData method is a wrapper of Args's Add method.

func (*FormData) AddDatas

func (f *FormData) AddDatas(m map[string][]string)

AddDatas method supports add multiple fields.

func (*FormData) DelDatas

func (f *FormData) DelDatas(key ...string)

DelDatas method deletes multiple fields.

func (*FormData) Reset

func (f *FormData) Reset()

Reset clear the FormData object.

func (*FormData) SetData

func (f *FormData) SetData(key, val string)

SetData method is a wrapper of Args's Set method.

func (*FormData) SetDatas

func (f *FormData) SetDatas(m map[string]string)

SetDatas method supports set multiple fields.

func (*FormData) SetDatasWithStruct

func (f *FormData) SetDatasWithStruct(v any)

SetDatasWithStruct method supports set multiple fields via a struct.

type Header struct {
	*fasthttp.RequestHeader
}

Header is a wrapper which wrap http.Header, the header in client and request will store in it.

func (*Header) AddHeaders

func (h *Header) AddHeaders(r map[string][]string)

AddHeaders receive a map and add each value to header.

func (*Header) PeekMultiple

func (h *Header) PeekMultiple(key string) []string

PeekMultiple methods returns multiple field in header with same key.

func (*Header) SetHeaders

func (h *Header) SetHeaders(r map[string]string)

SetHeaders will override all headers.

type PathParam

type PathParam map[string]string

PathParam is a map which to store the cookies.

func (PathParam) Add

func (p PathParam) Add(key, val string)

Add method impl the method in WithStruct interface.

func (PathParam) Del

func (p PathParam) Del(key string)

Del method impl the method in WithStruct interface.

func (PathParam) DelParams

func (p PathParam) DelParams(key ...string)

DelParams method deletes multiple val in PathParams.

func (PathParam) Reset

func (p PathParam) Reset()

Reset clear the PathParams object.

func (PathParam) SetParam

func (p PathParam) SetParam(key, val string)

SetParam method sets a single val in PathParam.

func (PathParam) SetParams

func (p PathParam) SetParams(m map[string]string)

SetParams method sets multiple val in PathParam.

func (PathParam) SetParamsWithStruct

func (p PathParam) SetParamsWithStruct(v any)

SetParamsWithStruct method sets multiple val in PathParam via a struct.

func (PathParam) VisitAll

func (p PathParam) VisitAll(f func(key, val string))

VisitAll method receive a function which can travel the all val.

type QueryParam

type QueryParam struct {
	*fasthttp.Args
}

QueryParam is a wrapper which wrap url.Values, the query string and formdata in client and request will store in it.

func (*QueryParam) AddParams

func (p *QueryParam) AddParams(r map[string][]string)

AddParams receive a map and add each value to param.

func (*QueryParam) SetParams

func (p *QueryParam) SetParams(r map[string]string)

SetParams will override all params.

func (*QueryParam) SetParamsWithStruct

func (p *QueryParam) SetParamsWithStruct(v any)

SetParamsWithStruct will override all params with struct or pointer of struct. Now nested structs are not currently supported.

type Request

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

Request is a struct which contains the request data.

func AcquireRequest

func AcquireRequest() *Request

AcquireRequest returns an empty request object from the pool.

The returned request may be returned to the pool with ReleaseRequest when no longer needed. This allows reducing GC load.

func (*Request) AddFile

func (r *Request) AddFile(path string) *Request

AddFile method adds single file field and its value in the request instance via file path.

func (*Request) AddFileWithReader

func (r *Request) AddFileWithReader(name string, reader io.ReadCloser) *Request

AddFileWithReader method adds single field and its value in the request instance via reader.

func (*Request) AddFiles

func (r *Request) AddFiles(files ...*File) *Request

AddFiles method adds multiple file fields and its value in the request instance via File instance.

func (*Request) AddFormData

func (r *Request) AddFormData(key, val string) *Request

AddFormData method adds a single form data field and its value in the request instance.

func (*Request) AddFormDatas

func (r *Request) AddFormDatas(m map[string][]string) *Request

AddFormDatas method adds multiple form data fields and its values in the request instance.

func (*Request) AddHeader

func (r *Request) AddHeader(key, val string) *Request

AddHeader method adds a single header field and its value in the request instance. It will override header which set in client instance.

func (*Request) AddHeaders

func (r *Request) AddHeaders(h map[string][]string) *Request

AddHeaders method adds multiple header fields and its values at one go in the request instance. It will override header which set in client instance.

func (*Request) AddParam

func (r *Request) AddParam(key, val string) *Request

AddParam method adds a single param field and its value in the request instance. It will override param which set in client instance.

func (*Request) AddParams

func (r *Request) AddParams(m map[string][]string) *Request

AddParams method adds multiple param fields and its values at one go in the request instance. It will override param which set in client instance.

func (*Request) Boundary

func (r *Request) Boundary() string

Boundary returns boundary in multipart boundary.

func (*Request) Client

func (r *Request) Client() *Client

Client get Client instance in Request.

func (*Request) Context

func (r *Request) Context() context.Context

Context returns the Context if its already set in request otherwise it creates new one using `context.Background()`.

func (*Request) Cookie

func (r *Request) Cookie(key string) string

Cookie returns the cookie be set in request instance. if cookie doesn't exist, return empty string.

func (*Request) Custom

func (r *Request) Custom(url, method string) (*Response, error)

Custom Send custom request.

func (*Request) DelCookies

func (r *Request) DelCookies(key ...string) *Request

DelCookies method deletes single or multiple cookie fields ant its values.

func (*Request) DelFormDatas

func (r *Request) DelFormDatas(key ...string) *Request

DelFormDatas method deletes multiple form data fields and its value in the request instance.

func (*Request) DelParams

func (r *Request) DelParams(key ...string) *Request

DelParams method deletes single or multiple param fields ant its values.

func (*Request) DelPathParams

func (r *Request) DelPathParams(key ...string) *Request

DelPathParams method deletes single or multiple path param fields ant its values.

func (*Request) Delete

func (r *Request) Delete(url string) (*Response, error)

Delete Send Delete request.

func (*Request) File

func (r *Request) File(name string) *File

File returns file ptr store in request obj by name. If name field is empty, it will try to match path.

func (*Request) FileByPath

func (r *Request) FileByPath(path string) *File

FileByPath returns file ptr store in request obj by path.

func (*Request) FormData

func (r *Request) FormData(key string) []string

FormData method returns form data value via key, this method will visit all field in the form data.

func (*Request) Get

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

Get Send get request.

func (*Request) Head

func (r *Request) Head(url string) (*Response, error)

Head Send head request.

func (*Request) Header

func (r *Request) Header(key string) []string

Header method returns header value via key, this method will visit all field in the header, then sort them.

func (*Request) MaxRedirects

func (r *Request) MaxRedirects() int

MaxRedirects returns the max redirects count in request.

func (*Request) Method

func (r *Request) Method() string

Method returns http method in request.

func (*Request) Options

func (r *Request) Options(url string) (*Response, error)

Options Send Options request.

func (*Request) Param

func (r *Request) Param(key string) []string

Param method returns params value via key, this method will visit all field in the query param.

func (*Request) Patch

func (r *Request) Patch(url string) (*Response, error)

Patch Send patch request.

func (*Request) PathParam

func (r *Request) PathParam(key string) string

PathParam returns the path param be set in request instance. if path param doesn't exist, return empty string.

func (*Request) Post

func (r *Request) Post(url string) (*Response, error)

Post Send post request.

func (*Request) Put

func (r *Request) Put(url string) (*Response, error)

Put Send put request.

func (*Request) Referer

func (r *Request) Referer() string

Referer returns referer in request instance.

func (*Request) Reset

func (r *Request) Reset()

Reset clear Request object, used by ReleaseRequest method.

func (*Request) ResetPathParams

func (r *Request) ResetPathParams() *Request

ResetPathParams deletes all path params.

func (*Request) Send

func (r *Request) Send() (*Response, error)

Send a request.

func (*Request) SetBoundary

func (r *Request) SetBoundary(b string) *Request

SetBoundary method sets multipart boundary.

func (*Request) SetClient

func (r *Request) SetClient(c *Client) *Request

SetClient method sets client in request instance.

func (*Request) SetContext

func (r *Request) SetContext(ctx context.Context) *Request

SetContext sets the context.Context for current Request. It allows to interrupt the request execution if ctx.Done() channel is closed. See https://blog.golang.org/context article and the "context" package documentation.

func (*Request) SetCookie

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

SetCookie method sets a single cookie field and its value in the request instance. It will override cookie which set in client instance.

func (*Request) SetCookies

func (r *Request) SetCookies(m map[string]string) *Request

SetCookies method sets multiple cookie fields and its values at one go in the request instance. It will override cookie which set in client instance.

func (*Request) SetCookiesWithStruct

func (r *Request) SetCookiesWithStruct(v any) *Request

SetCookiesWithStruct method sets multiple cookie fields and its values at one go in the request instance. It will override cookie which set in client instance.

func (*Request) SetFormData

func (r *Request) SetFormData(key, val string) *Request

SetFormData method sets a single form data field and its value in the request instance.

func (*Request) SetFormDatas

func (r *Request) SetFormDatas(m map[string]string) *Request

SetFormDatas method sets multiple form data fields and its values in the request instance.

func (*Request) SetFormDatasWithStruct

func (r *Request) SetFormDatasWithStruct(v any) *Request

SetFormDatasWithStruct method sets multiple form data fields and its values in the request instance via struct.

func (*Request) SetHeader

func (r *Request) SetHeader(key, val string) *Request

SetHeader method sets a single header field and its value in the request instance. It will override header which set in client instance.

func (*Request) SetHeaders

func (r *Request) SetHeaders(h map[string]string) *Request

SetHeaders method sets multiple header fields and its values at one go in the request instance. It will override header which set in client instance.

func (*Request) SetJSON

func (r *Request) SetJSON(v any) *Request

SetJSON method sets json body in request.

func (*Request) SetMaxRedirects

func (r *Request) SetMaxRedirects(count int) *Request

SetMaxRedirects method sets the maximum number of redirects at one go in the request instance. It will override max redirect which set in client instance.

func (*Request) SetMethod

func (r *Request) SetMethod(method string) *Request

SetMethod will set method for Request object, user should use request method to set method.

func (*Request) SetParam

func (r *Request) SetParam(key, val string) *Request

SetParam method sets a single param field and its value in the request instance. It will override param which set in client instance.

func (*Request) SetParams

func (r *Request) SetParams(m map[string]string) *Request

SetParams method sets multiple param fields and its values at one go in the request instance. It will override param which set in client instance.

func (*Request) SetParamsWithStruct

func (r *Request) SetParamsWithStruct(v any) *Request

SetParamsWithStruct method sets multiple param fields and its values at one go in the request instance. It will override param which set in client instance.

func (*Request) SetPathParam

func (r *Request) SetPathParam(key, val string) *Request

SetPathParam method sets a single path param field and its value in the request instance. It will override path param which set in client instance.

func (*Request) SetPathParams

func (r *Request) SetPathParams(m map[string]string) *Request

SetPathParams method sets multiple path param fields and its values at one go in the request instance. It will override path param which set in client instance.

func (*Request) SetPathParamsWithStruct

func (r *Request) SetPathParamsWithStruct(v any) *Request

SetPathParamsWithStruct method sets multiple path param fields and its values at one go in the request instance. It will override path param which set in client instance.

func (*Request) SetRawBody

func (r *Request) SetRawBody(v []byte) *Request

SetRawBody method sets body with raw data in request.

func (*Request) SetReferer

func (r *Request) SetReferer(referer string) *Request

SetReferer method sets referer in request. It will override referer which set in client instance.

func (*Request) SetTimeout

func (r *Request) SetTimeout(t time.Duration) *Request

SetTimeout method sets timeout field and its values at one go in the request instance. It will override timeout which set in client instance.

func (*Request) SetURL

func (r *Request) SetURL(url string) *Request

SetURL will set url for Request object.

func (*Request) SetUserAgent

func (r *Request) SetUserAgent(ua string) *Request

SetUserAgent method sets user agent in request. It will override user agent which set in client instance.

func (*Request) SetXML

func (r *Request) SetXML(v any) *Request

SetXML method sets xml body in request.

func (*Request) Timeout

func (r *Request) Timeout() time.Duration

Timeout returns the length of timeout in request.

func (*Request) URL

func (r *Request) URL() string

URL returns request url in Request instance.

func (*Request) UserAgent

func (r *Request) UserAgent() string

UserAgent returns user agent in request instance.

type RequestHook

type RequestHook func(*Client, *Request) error

RequestHook is a function that receives Agent and Request, it can change the data in Request and Agent.

Called before a request is sent.

type Response

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

Response is the result of a request. This object is used to access the response data.

func AcquireResponse

func AcquireResponse() *Response

AcquireResponse returns an empty response object from the pool.

The returned response may be returned to the pool with ReleaseResponse when no longer needed. This allows reducing GC load.

func Delete

func Delete(url string, cfg ...Config) (*Response, error)

Delete send a delete request use defaultClient, a convenient method.

func Get

func Get(url string, cfg ...Config) (*Response, error)

Get send a get request use defaultClient, a convenient method.

func Head(url string, cfg ...Config) (*Response, error)

Head send a head request use defaultClient, a convenient method.

func Options

func Options(url string, cfg ...Config) (*Response, error)

Options send a options request use defaultClient, a convenient method.

func Patch

func Patch(url string, cfg ...Config) (*Response, error)

Patch send a patch request use defaultClient, a convenient method.

func Post

func Post(url string, cfg ...Config) (*Response, error)

Post send a post request use defaultClient, a convenient method.

func Put

func Put(url string, cfg ...Config) (*Response, error)

Put send a put request use defaultClient, a convenient method.

func (*Response) Body

func (r *Response) Body() []byte

Body method returns HTTP response as []byte array for the executed request.

func (*Response) Close

func (r *Response) Close()

Close method will release Request object and Response object, after call Close please don't use these object.

func (*Response) Cookies

func (r *Response) Cookies() []*fasthttp.Cookie

Cookies method to access all the response cookies.

func (*Response) Header

func (r *Response) Header(key string) string

Header method returns the response headers.

func (*Response) JSON

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

JSON method will unmarshal body to json.

func (*Response) Protocol

func (r *Response) Protocol() string

Protocol method returns the HTTP response protocol used for the request.

func (*Response) Reset

func (r *Response) Reset()

Reset clear Response object.

func (*Response) Save

func (r *Response) Save(v any) error

Save method will save the body to a file or io.Writer.

func (*Response) Status

func (r *Response) Status() string

Status method returns the HTTP status string for the executed request.

func (*Response) StatusCode

func (r *Response) StatusCode() int

StatusCode method returns the HTTP status code for the executed request.

func (*Response) String

func (r *Response) String() string

String method returns the body of the server response as String.

func (*Response) XML

func (r *Response) XML(v any) error

XML method will unmarshal body to xml.

type ResponseHook

type ResponseHook func(*Client, *Response, *Request) error

ResponseHook is a function that receives Agent, Response and Request, it can change the data is Response or deal with some effects.

Called after a response has been received.

type RetryConfig

type RetryConfig = retry.Config

RetryConfig is an alias for config in the `addon/retry` package.

type SetFileFunc

type SetFileFunc func(f *File)

SetFileFunc The methods as follows is used by AcquireFile method. You can set file field via these method.

func SetFileFieldName

func SetFileFieldName(p string) SetFileFunc

SetFileFieldName method sets key of file in the body.

func SetFileName

func SetFileName(n string) SetFileFunc

SetFileName method sets file name.

func SetFilePath

func SetFilePath(p string) SetFileFunc

SetFilePath method set file path.

func SetFileReader

func SetFileReader(r io.ReadCloser) SetFileFunc

SetFileReader method can receive a io.ReadCloser

type WithStruct

type WithStruct interface {
	Add(name, obj string)
	Del(name string)
}

WithStruct Implementing this interface allows data to be stored from a struct via reflect.

Jump to

Keyboard shortcuts

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