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 AppendClientCertificates(certs ...tls.Certificate) *Client
- func AppendRootCAs(pemFilePath string) *Client
- func DisableProxy() *Client
- func DisableRedirect() *Client
- func DisableSession() *Client
- func DisableVerify() *Client
- func New() *Client
- func SetCookieJar(jar http.CookieJar) *Client
- func SetProxy(proxy func(*http.Request) (*stdurl.URL, error)) *Client
- func SetProxyFromURL(url string) *Client
- func SetRedirect(policy func(req *http.Request, via []*http.Request) error) *Client
- func SetRetry(attempts int, delay time.Duration, conditions ...func(*Response) bool) *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) AppendClientCertificates(certs ...tls.Certificate) *Client
- func (c *Client) AppendRootCAs(pemFilePath 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) SetProxy(proxy func(*http.Request) (*stdurl.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) SetRetry(attempts int, delay time.Duration, conditions ...func(*Response) bool) *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 File
- type Files
- type Form
- type H
- func (h H) Get(key string) interface{}
- func (h H) GetBool(key string) bool
- func (h H) GetBoolSlice(key string) []bool
- func (h H) GetFloat32(key string) float32
- func (h H) GetFloat32Slice(key string) []float32
- func (h H) GetFloat64(key string) float64
- func (h H) GetFloat64Slice(key string) []float64
- func (h H) GetH(key string) H
- func (h H) GetHSlice(key string) []H
- func (h H) GetInt(key string) int
- func (h H) GetInt32(key string) int32
- func (h H) GetInt32Slice(key string) []int32
- func (h H) GetInt64(key string) int64
- func (h H) GetInt64Slice(key string) []int64
- func (h H) GetIntSlice(key string) []int
- func (h H) GetSlice(key string) []interface{}
- func (h H) GetString(key string) string
- func (h H) GetStringSlice(key string) []string
- func (h H) GetUint(key string) uint
- func (h H) GetUint32(key string) uint32
- func (h H) GetUint32Slice(key string) []uint32
- func (h H) GetUint64(key string) uint64
- func (h H) GetUint64Slice(key string) []uint64
- func (h H) GetUintSlice(key string) []uint
- func (h H) String() string
- type Headers
- type KV
- 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 ...*http.Cookie) *Request
- func (req *Request) SetForm(form KV) *Request
- func (req *Request) SetHeaders(headers KV) *Request
- func (req *Request) SetHost(host string) *Request
- func (req *Request) SetJSON(data interface{}, escapeHTML bool) *Request
- func (req *Request) SetMultipart(files Files, form KV) *Request
- func (req *Request) SetQuery(params KV) *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) SetTimeout(timeout time.Duration) *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 ...*http.Cookie) RequestOption
- func WithForm(form KV) RequestOption
- func WithHeaders(headers KV) RequestOption
- func WithHost(host string) RequestOption
- func WithJSON(data interface{}, escapeHTML bool) RequestOption
- func WithMultipart(files Files, form KV) RequestOption
- func WithQuery(params KV) RequestOption
- func WithReferer(referer string) RequestOption
- func WithRetry(attempts int, delay time.Duration, conditions ...func(*Response) bool) RequestOption
- func WithText(text string) RequestOption
- func WithTimeout(timeout time.Duration) 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
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 timeout used by DefaultClient. DefaultTimeout = 120 * time.Second )
const (
// Version of sreq.
Version = "0.7.5"
)
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") // ErrNilContext can be used when the context is nil. ErrNilContext = errors.New("nil Context") // ErrNilCookieJar can be used when the cookie jar is nil. ErrNilCookieJar = errors.New("sreq: nil cookie jar") // ErrJarCookiesNotPresent can be used when cookies for a given URL not present in cookie jar. ErrJarCookiesNotPresent = errors.New("sreq: cookies for the given URL not present") // ErrJarNamedCookieNotPresent can be used when named cookie for a given URL not present in cookie jar. ErrJarNamedCookieNotPresent = errors.New("sreq: named cookie for the given URL not present") // ErrResponseCookiesNotPresent can be used when cookies of the HTTP response not present. ErrResponseCookiesNotPresent = errors.New("sreq: cookies not present") // ErrResponseNamedCookieNotPresent can be used when named cookie of the HTTP response not present. ErrResponseNamedCookieNotPresent = errors.New("sreq: named cookie not present") )
var ( // DefaultClient is the default sreq Client, // used for the global functions such as Get, Post, etc. DefaultClient = New() )
Functions ¶
func DefaultTransport ¶
DefaultTransport returns an HTTP transport used by DefaultClient. It's a clone of http.DefaultTransport indeed.
func FilterCookie ¶
FilterCookie returns the named cookie to send in a request for the given URL.
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 AppendClientCertificates ¶
func AppendClientCertificates(certs ...tls.Certificate) *Client
AppendClientCertificates appends client certificates to the HTTP client.
func AppendRootCAs ¶
AppendRootCAs appends root certificate authorities 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 SetProxyFromURL ¶
SetProxyFromURL sets proxy of the HTTP client from a url.
func SetRedirect ¶
SetRedirect sets policy of the HTTP client for handling redirects.
func SetRetry ¶
SetRetry sets retry policy of the client. The retry policy will be applied to all requests raised from this client instance. Also it can be overridden at request level retry policy options. Notes: Request timeout or context has priority over the retry policy.
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 ¶
func UseRequestInterceptors(interceptors ...RequestInterceptor) *Client
UseRequestInterceptors appends request interceptors of the client.
func UseResponseInterceptors ¶
func UseResponseInterceptors(interceptors ...ResponseInterceptor) *Client
UseResponseInterceptors appends response interceptors of the client.
func (*Client) AppendClientCertificates ¶
func (c *Client) AppendClientCertificates(certs ...tls.Certificate) *Client
AppendClientCertificates appends client certificates to the HTTP client.
func (*Client) AppendRootCAs ¶
AppendRootCAs appends root certificate authorities 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.
func (*Client) FilterCookies ¶
FilterCookies returns the cookies to send in a request for the given URL.
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) 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) SetRetry ¶
func (c *Client) SetRetry(attempts int, delay time.Duration, conditions ...func(*Response) bool) *Client
SetRetry sets retry policy of the client. The retry policy will be applied to all requests raised from this client instance. Also it can be overridden at request level retry policy options. Notes: Request timeout or context has priority over the retry policy.
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 ¶
func (c *Client) UseRequestInterceptors(interceptors ...RequestInterceptor) *Client
UseRequestInterceptors appends request interceptors of the client.
func (*Client) UseResponseInterceptors ¶
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 File ¶
File specifies a file. To upload a file you must specify its Filename field, otherwise sreq will raise a *RequestError and then abort request. If you don't specify the MIME field, sreq will detect automatically using http.DetectContentType.
func MustOpen ¶
MustOpen opens the named file and returns a *File instance whose Filename is filename. If there is an error, it will panic.
func (*File) SetFilename ¶
SetFilename sets Filename field value of f.
type Files ¶
Files maps a string key to a *File type value, used for files of multipart payload.
type H ¶
type H map[string]interface{}
H is a shortcut for map[string]interface{}, used for JSON unmarshalling.
func (H) GetBoolSlice ¶
GetBoolSlice gets the []bool value associated with the given key.
func (H) GetFloat32 ¶
GetFloat32 gets the float32 value associated with the given key.
func (H) GetFloat32Slice ¶
GetFloat32Slice gets the []float32 value associated with the given key.
func (H) GetFloat64 ¶
GetFloat64 gets the float64 value associated with the given key.
func (H) GetFloat64Slice ¶
GetFloat64Slice gets the []float64 value associated with the given key.
func (H) GetInt32Slice ¶
GetInt32Slice gets the []int32 value associated with the given key.
func (H) GetInt64Slice ¶
GetInt64Slice gets the []int64 value associated with the given key.
func (H) GetIntSlice ¶
GetIntSlice gets the []int value associated with the given key.
func (H) GetStringSlice ¶
GetStringSlice gets the []string value associated with the given key.
func (H) GetUint32Slice ¶
GetUint32Slice gets the []uint32 value associated with the given key.
func (H) GetUint64Slice ¶
GetUint64Slice gets the []uint64 value associated with the given key.
func (H) GetUintSlice ¶
GetUintSlice gets the []uint value associated with the given key.
type Headers ¶
type Headers map[string]interface{}
Headers maps a string key to an interface{} type value, support string, int, []string, []int or []interface{} only with string and int. Used for headers.
func (Headers) Get ¶
Get gets the value associated with the given key, ignore unsupported data type.
type KV ¶
KV is the interface that defines a data type used by sreq in many cases. The Keys method should return a slice of keys typed string. The Get method should return a slice of values typed string associated with the given key.
type Request ¶
type Request struct { RawRequest *http.Request Err error // 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 does 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 ¶
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 timeout or context has priority over the retry policy.
func (*Request) SetTimeout ¶
SetTimeout sets timeout for the HTTP request.
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 ¶
RequestInterceptor specifies a request interceptor.
type RequestOption ¶
RequestOption specifies a request options, like params, form, etc.
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 does 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 ...*http.Cookie) RequestOption
WithCookies appends cookies for the HTTP request.
func WithForm ¶
func WithForm(form KV) RequestOption
WithForm sets form payload for the HTTP request.
func WithHeaders ¶
func WithHeaders(headers KV) 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 ¶
func WithMultipart(files Files, form KV) 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 KV) 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 timeout or context has priority over the retry policy.
func WithText ¶
func WithText(text string) RequestOption
WithText sets plain text payload for the HTTP request.
func WithTimeout ¶
func WithTimeout(timeout time.Duration) RequestOption
WithTimeout sets timeout for the HTTP request.
func WithUserAgent ¶
func WithUserAgent(userAgent string) RequestOption
WithUserAgent sets User-Agent header value for the HTTP request.
func WithXML ¶
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 ¶
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.
func (*Response) Save ¶
Save saves the HTTP response into a file. Notes: Save won't make the HTTP response body reused.
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 ¶
ResponseInterceptor specifies a response interceptor.
type Values ¶
type Values map[string]interface{}
Values maps a string key to an interface{} type value, support string, int, []string, []int or []interface{} only with string and int. Used for query parameters and form values.