Documentation ¶
Index ¶
- Constants
- Variables
- func DefaultTransport() *http.Transport
- func FilterCookie(url string, name string) (*http.Cookie, error)
- func FilterCookies(url string) ([]*http.Cookie, error)
- type Client
- func AppendClientCerts(certs ...tls.Certificate) *Client
- func AppendRootCerts(pemFile string) *Client
- func DisableProxy() *Client
- func DisableRedirect() *Client
- func DisableSession() *Client
- func DisableVerify() *Client
- func New() *Client
- func SetCookieJar(jar http.CookieJar) *Client
- func SetCookies(url string, cookies ...*http.Cookie) *Client
- func SetProxy(proxy func(*http.Request) (*neturl.URL, error)) *Client
- func SetProxyFromURL(url string) *Client
- func SetRedirect(policy func(req *http.Request, via []*http.Request) error) *Client
- func SetTLSClientConfig(config *tls.Config) *Client
- func SetTimeout(timeout time.Duration) *Client
- func SetTransport(transport http.RoundTripper) *Client
- func UseRequestInterceptors(interceptors ...RequestInterceptor) *Client
- func UseResponseInterceptors(interceptors ...ResponseInterceptor) *Client
- func (c *Client) AppendClientCerts(certs ...tls.Certificate) *Client
- func (c *Client) AppendRootCerts(pemFile string) *Client
- func (c *Client) Delete(url string, opts ...RequestOption) *Response
- func (c *Client) DisableProxy() *Client
- func (c *Client) DisableRedirect() *Client
- func (c *Client) DisableSession() *Client
- func (c *Client) DisableVerify() *Client
- func (c *Client) Do(req *Request) *Response
- func (c *Client) FilterCookie(url string, name string) (*http.Cookie, error)
- func (c *Client) FilterCookies(url string) ([]*http.Cookie, error)
- func (c *Client) Get(url string, opts ...RequestOption) *Response
- func (c *Client) Head(url string, opts ...RequestOption) *Response
- func (c *Client) Patch(url string, opts ...RequestOption) *Response
- func (c *Client) Post(url string, opts ...RequestOption) *Response
- func (c *Client) Put(url string, opts ...RequestOption) *Response
- func (c *Client) Raw() (*http.Client, error)
- func (c *Client) Send(method string, url string, opts ...RequestOption) *Response
- func (c *Client) SetCookieJar(jar http.CookieJar) *Client
- func (c *Client) SetCookies(url string, cookies ...*http.Cookie) *Client
- func (c *Client) SetProxy(proxy func(*http.Request) (*neturl.URL, error)) *Client
- func (c *Client) SetProxyFromURL(url string) *Client
- func (c *Client) SetRedirect(policy func(req *http.Request, via []*http.Request) error) *Client
- func (c *Client) SetTLSClientConfig(config *tls.Config) *Client
- func (c *Client) SetTimeout(timeout time.Duration) *Client
- func (c *Client) SetTransport(transport http.RoundTripper) *Client
- func (c *Client) UseRequestInterceptors(interceptors ...RequestInterceptor) *Client
- func (c *Client) UseResponseInterceptors(interceptors ...ResponseInterceptor) *Client
- type ClientError
- type Cookies
- func (c Cookies) Clone() Cookies
- func (c Cookies) Decode() []*http.Cookie
- func (c Cookies) Del(name string)
- func (c Cookies) Get(name string) *http.Cookie
- func (c Cookies) Merge(c2 Cookies)
- func (c Cookies) Set(name string, value string)
- func (c Cookies) SetDefault(name string, value string)
- func (c Cookies) Update(c2 Cookies)
- type File
- type Files
- type Form
- type H
- func (h H) Decode(output interface{}) error
- func (h H) Get(key string) interface{}
- func (h H) GetBool(key string) bool
- func (h H) GetBoolDefault(key string, defaultValue bool) bool
- func (h H) GetBoolSlice(key string) []bool
- func (h H) GetH(key string) H
- func (h H) GetHSlice(key string) []H
- func (h H) GetNumber(key string) Number
- func (h H) GetNumberDefault(key string, defaultValue Number) Number
- func (h H) GetNumberSlice(key string) []Number
- func (h H) GetSlice(key string) []interface{}
- func (h H) GetString(key string) string
- func (h H) GetStringDefault(key string, defaultValue string) string
- func (h H) GetStringSlice(key string) []string
- func (h H) String() string
- type Headers
- type Number
- func (n Number) Float32() float32
- func (n Number) Float64() float64
- func (n Number) Int() int
- func (n Number) Int16() int16
- func (n Number) Int32() int32
- func (n Number) Int64() int64
- func (n Number) Int8() int8
- func (n Number) String() string
- func (n Number) Uint() uint
- func (n Number) Uint16() uint16
- func (n Number) Uint32() uint32
- func (n Number) Uint64() uint64
- func (n Number) Uint8() uint8
- type Params
- type Request
- func (req *Request) Raw() (*http.Request, error)
- func (req *Request) SetBasicAuth(username string, password string) *Request
- func (req *Request) SetBearerToken(token string) *Request
- func (req *Request) SetBody(body io.Reader) *Request
- func (req *Request) SetContent(content []byte) *Request
- func (req *Request) SetContentType(contentType string) *Request
- func (req *Request) SetContext(ctx context.Context) *Request
- func (req *Request) SetCookies(cookies Cookies) *Request
- func (req *Request) SetForm(form Form) *Request
- func (req *Request) SetHeaders(headers Headers) *Request
- func (req *Request) SetHost(host string) *Request
- func (req *Request) SetJSON(data interface{}, escapeHTML bool) *Request
- func (req *Request) SetMultipart(files Files, form Form) *Request
- func (req *Request) SetQuery(params Params) *Request
- func (req *Request) SetReferer(referer string) *Request
- func (req *Request) SetRetry(attempts int, delay time.Duration, conditions ...func(*Response) bool) *Request
- func (req *Request) SetText(text string) *Request
- func (req *Request) SetUserAgent(userAgent string) *Request
- func (req *Request) SetXML(data interface{}) *Request
- type RequestError
- type RequestInterceptor
- type RequestOption
- func WithBasicAuth(username string, password string) RequestOption
- func WithBearerToken(token string) RequestOption
- func WithBody(body io.Reader) RequestOption
- func WithContent(content []byte) RequestOption
- func WithContentType(contentType string) RequestOption
- func WithContext(ctx context.Context) RequestOption
- func WithCookies(cookies Cookies) RequestOption
- func WithForm(form Form) RequestOption
- func WithHeaders(headers Headers) RequestOption
- func WithHost(host string) RequestOption
- func WithJSON(data interface{}, escapeHTML bool) RequestOption
- func WithMultipart(files Files, form Form) RequestOption
- func WithQuery(params Params) RequestOption
- func WithReferer(referer string) RequestOption
- func WithRetry(attempts int, delay time.Duration, conditions ...func(*Response) bool) RequestOption
- func WithText(text string) RequestOption
- func WithUserAgent(userAgent string) RequestOption
- func WithXML(data interface{}) RequestOption
- type Response
- func Delete(url string, opts ...RequestOption) *Response
- func Do(req *Request) *Response
- func Get(url string, opts ...RequestOption) *Response
- func Head(url string, opts ...RequestOption) *Response
- func Patch(url string, opts ...RequestOption) *Response
- func Post(url string, opts ...RequestOption) *Response
- func Put(url string, opts ...RequestOption) *Response
- func Send(method string, url string, opts ...RequestOption) *Response
- func (resp *Response) Content() ([]byte, error)
- func (resp *Response) Cookie(name string) (*http.Cookie, error)
- func (resp *Response) Cookies() ([]*http.Cookie, error)
- func (resp *Response) EnsureStatus(code int) *Response
- func (resp *Response) EnsureStatus2xx() *Response
- func (resp *Response) EnsureStatusOk() *Response
- func (resp *Response) H() (H, error)
- func (resp *Response) JSON(v interface{}) error
- func (resp *Response) Raw() (*http.Response, error)
- func (resp *Response) Save(filename string, perm os.FileMode) error
- func (resp *Response) Text(e ...encoding.Encoding) (string, error)
- func (resp *Response) Verbose(w io.Writer) error
- func (resp *Response) XML(v interface{}) error
- type ResponseInterceptor
- type Values
- func (v Values) Decode() map[string][]string
- func (v Values) Del(key string)
- func (v Values) Encode(urlEscaped bool) string
- func (v Values) Get(key string) []string
- func (v Values) Marshal() string
- func (v Values) Merge(v2 Values)
- func (v Values) Set(key string, value interface{})
- func (v Values) SetDefault(key string, value interface{})
- func (v Values) Update(v2 Values)
Constants ¶
const ( // MethodGet represents the GET method for HTTP. MethodGet = "GET" // MethodHead represents the HEAD method for HTTP. MethodHead = "HEAD" // MethodPost represents the POST method for HTTP. MethodPost = "POST" // MethodPut represents the PUT method for HTTP. MethodPut = "PUT" // MethodPatch represents the PATCH method for HTTP. MethodPatch = "PATCH" // MethodDelete represents the DELETE method for HTTP. MethodDelete = "DELETE" // MethodConnect represents the CONNECT method for HTTP. MethodConnect = "CONNECT" // MethodOptions represents the OPTIONS method for HTTP. MethodOptions = "OPTIONS" // MethodTrace represents the TRACE method for HTTP. MethodTrace = "TRACE" )
const ( // DefaultTimeout is the preset timeout. DefaultTimeout = 120 * time.Second )
const (
// Version of sreq.
Version = "0.8.11"
)
Variables ¶
var ( // ErrUnexpectedTransport can be used if assert a RoundTripper as a non-nil *http.Transport instance failed. ErrUnexpectedTransport = errors.New("current transport isn't a non-nil *http.Transport instance") // ErrNilCookieJar can be used when the cookie jar is nil. ErrNilCookieJar = errors.New("nil cookie jar") // ErrNoCookie can be used when a cookie not found in the HTTP response or cookie jar. ErrNoCookie = errors.New("named cookie not present") )
var ( // GlobalClient is a sreq Client used by the global functions such as Get, Post, etc. GlobalClient = New() )
Functions ¶
func DefaultTransport ¶
DefaultTransport returns a preset HTTP transport. It's a clone of http.DefaultTransport indeed.
func FilterCookie ¶
FilterCookie returns the named cookie to send in a request for the given URL from cookie jar.
Types ¶
type Client ¶
Client wraps the raw HTTP client. Do not modify the client across Goroutines! You should reuse it as possible after initialized.
func AppendClientCerts ¶ added in v0.7.11
func AppendClientCerts(certs ...tls.Certificate) *Client
AppendClientCerts appends client certificates to the HTTP client.
func AppendRootCerts ¶ added in v0.7.12
AppendRootCerts appends root certificates from a pem file to the HTTP client.
func DisableRedirect ¶
func DisableRedirect() *Client
DisableRedirect makes the HTTP client not follow redirects.
func DisableSession ¶
func DisableSession() *Client
DisableSession makes the HTTP client not use cookie jar. Only use if you don't want to keep session for the next HTTP request.
func DisableVerify ¶
func DisableVerify() *Client
DisableVerify makes the HTTP client not verify the server's TLS certificate.
func SetCookieJar ¶
SetCookieJar sets cookie jar of the HTTP client.
func SetCookies ¶
SetCookies sets cookies to cookie jar for the given URL.
func SetProxyFromURL ¶
SetProxyFromURL sets proxy of the HTTP client from a url.
func SetRedirect ¶
SetRedirect sets policy of the HTTP client for handling redirects.
func SetTLSClientConfig ¶
SetTLSClientConfig sets TLS configuration of the HTTP client.
func SetTimeout ¶
SetTimeout sets timeout of the HTTP client.
func SetTransport ¶
func SetTransport(transport http.RoundTripper) *Client
SetTransport sets transport of the HTTP client.
func UseRequestInterceptors ¶ added in v0.2.0
func UseRequestInterceptors(interceptors ...RequestInterceptor) *Client
UseRequestInterceptors appends request interceptors of the client.
func UseResponseInterceptors ¶ added in v0.2.0
func UseResponseInterceptors(interceptors ...ResponseInterceptor) *Client
UseResponseInterceptors appends response interceptors of the client.
func (*Client) AppendClientCerts ¶ added in v0.7.11
func (c *Client) AppendClientCerts(certs ...tls.Certificate) *Client
AppendClientCerts appends client certificates to the HTTP client.
func (*Client) AppendRootCerts ¶ added in v0.7.12
AppendRootCerts appends root certificates from a pem file to the HTTP client.
func (*Client) Delete ¶
func (c *Client) Delete(url string, opts ...RequestOption) *Response
Delete makes a DELETE HTTP request.
func (*Client) DisableProxy ¶
DisableProxy makes the HTTP client not use proxy.
func (*Client) DisableRedirect ¶
DisableRedirect makes the HTTP client not follow redirects.
func (*Client) DisableSession ¶
DisableSession makes the HTTP client not use cookie jar. Only use if you don't want to keep session for the next HTTP request.
func (*Client) DisableVerify ¶
DisableVerify makes the HTTP client not verify the server's TLS certificate.
func (*Client) FilterCookie ¶
FilterCookie returns the named cookie to send in a request for the given URL from cookie jar.
func (*Client) FilterCookies ¶
FilterCookies returns the cookies to send in a request for the given URL from cookie jar.
func (*Client) Get ¶
func (c *Client) Get(url string, opts ...RequestOption) *Response
Get makes a GET HTTP request.
func (*Client) Head ¶
func (c *Client) Head(url string, opts ...RequestOption) *Response
Head makes a HEAD HTTP request.
func (*Client) Patch ¶
func (c *Client) Patch(url string, opts ...RequestOption) *Response
Patch makes a PATCH HTTP request.
func (*Client) Post ¶
func (c *Client) Post(url string, opts ...RequestOption) *Response
Post makes a POST HTTP request.
func (*Client) Put ¶
func (c *Client) Put(url string, opts ...RequestOption) *Response
Put makes a PUT HTTP request.
func (*Client) Send ¶
func (c *Client) Send(method string, url string, opts ...RequestOption) *Response
Send makes an HTTP request using a specified method.
func (*Client) SetCookieJar ¶
SetCookieJar sets cookie jar of the HTTP client.
func (*Client) SetCookies ¶
SetCookies sets cookies to cookie jar for the given URL.
func (*Client) SetProxyFromURL ¶
SetProxyFromURL sets proxy of the HTTP client from a url.
func (*Client) SetRedirect ¶
SetRedirect sets policy of the HTTP client for handling redirects.
func (*Client) SetTLSClientConfig ¶
SetTLSClientConfig sets TLS configuration of the HTTP client.
func (*Client) SetTimeout ¶
SetTimeout sets timeout of the HTTP client.
func (*Client) SetTransport ¶
func (c *Client) SetTransport(transport http.RoundTripper) *Client
SetTransport sets transport of the HTTP client.
func (*Client) UseRequestInterceptors ¶ added in v0.2.0
func (c *Client) UseRequestInterceptors(interceptors ...RequestInterceptor) *Client
UseRequestInterceptors appends request interceptors of the client.
func (*Client) UseResponseInterceptors ¶ added in v0.2.0
func (c *Client) UseResponseInterceptors(interceptors ...ResponseInterceptor) *Client
UseResponseInterceptors appends response interceptors of the client.
type ClientError ¶
ClientError records a client error, can be used when sreq builds Client failed.
func (*ClientError) Unwrap ¶
func (c *ClientError) Unwrap() error
Unwrap unpacks and returns the wrapped err of c.
type Cookies ¶ added in v0.8.8
Cookies is a shortcut for map[string]string, used for request cookies.
func (Cookies) Decode ¶ added in v0.8.8
Decode translates c and returns the equivalent request cookies.
func (Cookies) SetDefault ¶ added in v0.8.8
SetDefault sets the key to value if the value not exists.
type File ¶ added in v0.6.0
File specifies a file. To upload a file you must specify its Filename field, otherwise sreq will raise a *RequestError. If you don't specify the MIME field, sreq will detect automatically using http.DetectContentType.
func MustOpen ¶ added in v0.3.0
MustOpen opens the named file and returns a *File instance. If there is an error, it will panic.
func (*File) SetFilename ¶ added in v0.6.0
SetFilename sets Filename field value of f.
type H ¶ added in v0.7.0
type H map[string]interface{}
H is a shortcut for map[string]interface{}, used for JSON unmarshalling. Do not use it for other purposes!
func (H) Decode ¶ added in v0.8.7
Decode encodes h to JSON and then decodes to the output structure. output must be a pointer.
func (H) GetBool ¶ added in v0.7.0
GetBool gets the bool value associated with key. The zero value is returned if the key not exists.
func (H) GetBoolDefault ¶ added in v0.8.0
GetBoolDefault gets the bool value associated with key. The defaultValue is returned if the key not exists.
func (H) GetBoolSlice ¶ added in v0.7.0
GetBoolSlice gets the []bool value associated with key.
func (H) GetNumber ¶ added in v0.8.7
GetNumber gets the Number value associated with key. The zero value is returned if the key not exists.
func (H) GetNumberDefault ¶ added in v0.8.7
GetNumberDefault gets the Number value associated with key. The defaultValue is returned if the key not exists.
func (H) GetNumberSlice ¶ added in v0.8.7
GetNumberSlice gets the []Number value associated with key.
func (H) GetString ¶ added in v0.7.0
GetString gets the string value associated with key. The zero value is returned if the key not exists.
func (H) GetStringDefault ¶ added in v0.8.0
GetStringDefault gets the string value associated with key. The defaultValue is returned if the key not exists.
func (H) GetStringSlice ¶ added in v0.7.0
GetStringSlice gets the []string value associated with key.
type Number ¶ added in v0.8.7
type Number float64
Number is a shortcut for float64.
type Params ¶
type Params = Values
Params is an alias of Values, used for for request query parameters.
type Request ¶
type Request struct { RawRequest *http.Request Err error Body io.Reader Host string Headers Headers Params Params Form Form Cookies Cookies // contains filtered or unexported fields }
Request wraps the raw HTTP request.
func NewRequest ¶
NewRequest returns a new Request given a method, URL.
func (*Request) SetBasicAuth ¶
SetBasicAuth sets basic authentication for the HTTP request.
func (*Request) SetBearerToken ¶
SetBearerToken sets bearer token for the HTTP request.
func (*Request) SetBody ¶
SetBody sets body for the HTTP request. Notes: SetBody may not support retry since it's unable to read a stream twice.
func (*Request) SetContent ¶
SetContent sets bytes payload for the HTTP request.
func (*Request) SetContentType ¶
SetContentType sets Content-Type header value for the HTTP request.
func (*Request) SetContext ¶
SetContext sets context for the HTTP request.
func (*Request) SetCookies ¶
SetCookies sets cookies for the HTTP request.
func (*Request) SetHeaders ¶
SetHeaders sets headers for the HTTP request.
func (*Request) SetMultipart ¶ added in v0.3.0
SetMultipart sets multipart payload for the HTTP request. Notes: SetMultipart does not support retry since it's unable to read a stream twice.
func (*Request) SetReferer ¶
SetReferer sets Referer header value for the HTTP request.
func (*Request) SetRetry ¶
func (req *Request) SetRetry(attempts int, delay time.Duration, conditions ...func(*Response) bool) *Request
SetRetry sets retry policy for the HTTP request. Notes: Request context has priority over the retry policy.
func (*Request) SetUserAgent ¶
SetUserAgent sets User-Agent header value for the HTTP request.
type RequestError ¶
RequestError records a request error, can be used when sreq builds Request failed.
func (*RequestError) Error ¶
func (req *RequestError) Error() string
Error implements error interface.
func (*RequestError) Unwrap ¶
func (req *RequestError) Unwrap() error
Unwrap unpacks and returns the wrapped err of req.
type RequestInterceptor ¶ added in v0.2.0
RequestInterceptor specifies a request interceptor. If the returned error isn't nil, sreq will not send the request.
type RequestOption ¶
RequestOption specifies a request options, used to setup Request.
func WithBasicAuth ¶
func WithBasicAuth(username string, password string) RequestOption
WithBasicAuth sets basic authentication for the HTTP request.
func WithBearerToken ¶
func WithBearerToken(token string) RequestOption
WithBearerToken sets bearer token for the HTTP request.
func WithBody ¶
func WithBody(body io.Reader) RequestOption
WithBody sets body for the HTTP request. Notes: WithBody may not support retry since it's unable to read a stream twice.
func WithContent ¶
func WithContent(content []byte) RequestOption
WithContent sets bytes payload for the HTTP request.
func WithContentType ¶
func WithContentType(contentType string) RequestOption
WithContentType sets Content-Type header value for the HTTP request.
func WithContext ¶
func WithContext(ctx context.Context) RequestOption
WithContext sets context for the HTTP request.
func WithCookies ¶
func WithCookies(cookies Cookies) RequestOption
WithCookies appends cookies for the HTTP request.
func WithForm ¶
func WithForm(form Form) RequestOption
WithForm sets form payload for the HTTP request.
func WithHeaders ¶
func WithHeaders(headers Headers) RequestOption
WithHeaders sets headers for the HTTP request.
func WithJSON ¶
func WithJSON(data interface{}, escapeHTML bool) RequestOption
WithJSON sets JSON payload for the HTTP request.
func WithMultipart ¶ added in v0.3.0
func WithMultipart(files Files, form Form) RequestOption
WithMultipart sets multipart payload for the HTTP request. Notes: WithMultipart does not support retry since it's unable to read a stream twice.
func WithQuery ¶
func WithQuery(params Params) RequestOption
WithQuery sets query params for the HTTP request.
func WithReferer ¶
func WithReferer(referer string) RequestOption
WithReferer sets Referer header value for the HTTP request.
func WithRetry ¶
WithRetry sets retry policy for the HTTP request. Notes: Request context has priority over the retry policy.
func WithText ¶
func WithText(text string) RequestOption
WithText sets plain text payload for the HTTP request.
func WithUserAgent ¶
func WithUserAgent(userAgent string) RequestOption
WithUserAgent sets User-Agent header value for the HTTP request.
func WithXML ¶ added in v0.6.0
func WithXML(data interface{}) RequestOption
WithXML sets XML payload for the HTTP request.
type Response ¶
type Response struct { RawResponse *http.Response Err error // contains filtered or unexported fields }
Response wraps the raw HTTP response.
func Delete ¶
func Delete(url string, opts ...RequestOption) *Response
Delete makes a DELETE HTTP request.
func Patch ¶
func Patch(url string, opts ...RequestOption) *Response
Patch makes a PATCH HTTP request.
func Send ¶
func Send(method string, url string, opts ...RequestOption) *Response
Send makes an HTTP request using a specified method.
func (*Response) EnsureStatus ¶
EnsureStatus ensures the HTTP response's status code must be the code parameter.
func (*Response) EnsureStatus2xx ¶
EnsureStatus2xx ensures the HTTP response's status code must be 2xx.
func (*Response) EnsureStatusOk ¶
EnsureStatusOk ensures the HTTP response's status code must be 200.
func (*Response) H ¶ added in v0.7.0
H decodes the HTTP response body and unmarshals its JSON-encoded data into an H instance.
func (*Response) JSON ¶
JSON decodes the HTTP response body and unmarshals its JSON-encoded data into v. v must be a pointer.
func (*Response) Save ¶
Save saves the HTTP response into a file. Notes: Save won't make the HTTP response body reusable.
func (*Response) Text ¶
Text decodes the HTTP response body and returns the text representation of its raw data given an optional charset encoding.
type ResponseInterceptor ¶ added in v0.2.0
ResponseInterceptor specifies a response interceptor.
type Values ¶ added in v0.6.0
type Values map[string]interface{}
Values maps a string key to an interface{} type value, It's typically used for request query parameters, form data and headers.
func (Values) Decode ¶ added in v0.8.8
Decode translates v and returns the equivalent request query parameters, form data or headers.
func (Values) Encode ¶ added in v0.6.0
Encode encodes v into URL form sorted by key when v is considered as request query parameters or form data.
func (Values) Get ¶ added in v0.6.0
Get gets the equivalent request query parameter, form data or header value associated with key.
func (Values) SetDefault ¶ added in v0.8.1
SetDefault sets the key to value if the value not exists.