Documentation
¶
Index ¶
- Constants
- Variables
- func CopyHeader(h http.Header) http.Header
- func CopyURL(u *url.URL) (URL *url.URL, err error)
- func Download(rawurl string, toFile string) error
- func GetHeader() http.Header
- func GetHeaderSingle(key string) string
- func GetProxyURL() *url.URL
- func GetProxyUrl() string
- func GetUserAgent() string
- func MergeHeaders(h1, h2 http.Header) http.Header
- func MergeQueryString(parsedURL *url.URL, parsedQuery string) (*url.URL, error)
- func NewCookieJar() (http.CookieJar, error)
- func ReplaceQueryString(parsedURL *url.URL, parsedQuery string) (*url.URL, error)
- func WrapErr(err error, msg string) error
- func WrapErrf(err error, format string, args ...interface{}) error
- type Cookies
- type Error
- type HandlerFunc
- type OptionFunc
- func OptionBasicAuth(username, password string) OptionFunc
- func OptionBody(v interface{}) OptionFunc
- func OptionBodyFormData(v interface{}) OptionFunc
- func OptionBodyJson(v interface{}) OptionFunc
- func OptionCookies(cookies Cookies) OptionFunc
- func OptionHeader(v interface{}) OptionFunc
- func OptionHeaderSingle(k, v string) OptionFunc
- func OptionProxy(v interface{}) OptionFunc
- func OptionQueryString(v interface{}) OptionFunc
- func OptionRedirectNum(num int) OptionFunc
- func OptionTimeout(v time.Duration) OptionFunc
- type RedirectError
- type Request
- func (r *Request) BasicAuth() (username, password string, ok bool)
- func (req *Request) Context() context.Context
- func (req *Request) Copy() *Request
- func (req *Request) GetHeader() http.Header
- func (req *Request) GetHeaderSingle(key string) string
- func (req *Request) GetMethod() string
- func (req *Request) GetProxyURL() *url.URL
- func (req *Request) GetProxyUrl() string
- func (req *Request) GetTimeout() time.Duration
- func (req *Request) GetURL() *url.URL
- func (req *Request) GetUrl() string
- func (req *Request) GetUserAgent() string
- func (r *Request) SetBasicAuth(username, password string)
- func (req *Request) SetBody(params interface{}) *Request
- func (req *Request) SetBodyFormData(params interface{}) *Request
- func (req *Request) SetBodyJson(params interface{}) *Request
- func (req *Request) SetBodyXWwwFormUrlencoded(params interface{}) *Request
- func (req *Request) SetBodyXml(params interface{}) *Request
- func (req *Request) SetCharset(charset string) *Request
- func (req *Request) SetCookies(cookies Cookies) *Request
- func (req *Request) SetHeader(header http.Header) *Request
- func (req *Request) SetHeaderSingle(key, val string) *Request
- func (req *Request) SetHeaders(header http.Header) *Request
- func (req *Request) SetHost(host string) *Request
- func (req *Request) SetMethod(method string) *Request
- func (req *Request) SetProxyURL(u *url.URL) *Request
- func (req *Request) SetProxyUrl(rawurl string) *Request
- func (req *Request) SetQueryString(params interface{}) *Request
- func (req *Request) SetReferer(referer string) *Request
- func (req *Request) SetTimeout(t time.Duration) *Request
- func (req *Request) SetURL(u *url.URL) *Request
- func (req *Request) SetUrl(rawurl string) *Request
- func (req *Request) SetUserAgent(ua string) *Request
- func (req *Request) WithContext(ctx context.Context) *Request
- type Response
- func BuildResponse(resp *http.Response) (*Response, error)
- func Connect(rawurl string, ops ...OptionFunc) (*Response, error)
- func Delete(rawurl string, ops ...OptionFunc) (*Response, error)
- func Do(req *http.Request) (*Response, error)
- func Get(rawurl string, ops ...OptionFunc) (*Response, error)
- func Head(rawurl string, ops ...OptionFunc) (*Response, error)
- func Options(rawurl string, ops ...OptionFunc) (*Response, error)
- func Patch(rawurl string, ops ...OptionFunc) (*Response, error)
- func Post(rawurl string, ops ...OptionFunc) (*Response, error)
- func PostFormData(rawurl string, ops ...OptionFunc) (*Response, error)
- func Put(rawurl string, ops ...OptionFunc) (*Response, error)
- func Suck(req *Request, ops ...OptionFunc) (*Response, error)
- func Trace(rawurl string, ops ...OptionFunc) (*Response, error)
- func (r *Response) GetBody() []byte
- func (r *Response) GetContextType() string
- func (r *Response) GetHeader() http.Header
- func (r *Response) GetHeaderSingle(key string) string
- func (r *Response) GetHtml() string
- func (r *Response) GetJson(v interface{}) error
- func (r *Response) GetXml(v interface{}) error
- func (r *Response) Read(p []byte) (n int, err error)
- func (r *Response) String() string
- type Session
- func InsecureSkipVerify(skip bool) *Session
- func NewSession(options ...*SessionOptions) *Session
- func SetCheckRedirectHandler(handler func(req *http.Request, via []*http.Request) error) *Session
- func SetCookieJar(jar http.CookieJar) *Session
- func SetHeader(h http.Header) *Session
- func SetHeaderSingle(key, val string) *Session
- func SetProxyHandler(handler func(req *http.Request) (*url.URL, error)) *Session
- func SetProxyURL(u *url.URL) *Session
- func SetProxyUrl(rawurl string) *Session
- func SetTimeout(t time.Duration) *Session
- func SetUserAgent(ua string) *Session
- func Use(middleware ...HandlerFunc) *Session
- func (session *Session) Connect(rawurl string, ops ...OptionFunc) (*Response, error)
- func (session *Session) Cookies(rawurl string) Cookies
- func (session *Session) Delete(rawurl string, ops ...OptionFunc) (*Response, error)
- func (session *Session) Do(req *http.Request) (*Response, error)
- func (session *Session) Download(rawurl string, toFile string) error
- func (session *Session) Get(rawurl string, ops ...OptionFunc) (*Response, error)
- func (session *Session) GetHeader() http.Header
- func (session *Session) GetHeaderSingle(key string) string
- func (session *Session) GetProxyURL() *url.URL
- func (session *Session) GetProxyUrl() string
- func (session *Session) GetUserAgent() string
- func (session *Session) Head(rawurl string, ops ...OptionFunc) (*Response, error)
- func (session *Session) InsecureSkipVerify(skip bool) *Session
- func (session *Session) Options(rawurl string, ops ...OptionFunc) (*Response, error)
- func (session *Session) Patch(rawurl string, ops ...OptionFunc) (*Response, error)
- func (session *Session) Post(rawurl string, ops ...OptionFunc) (*Response, error)
- func (session *Session) PostFormData(rawurl string, ops ...OptionFunc) (*Response, error)
- func (session *Session) Put(rawurl string, ops ...OptionFunc) (*Response, error)
- func (session *Session) SetCheckRedirectHandler(handler func(req *http.Request, via []*http.Request) error) *Session
- func (session *Session) SetCookieJar(jar http.CookieJar) *Session
- func (session *Session) SetCookies(rawurl string, cookies Cookies)
- func (session *Session) SetHeader(h http.Header) *Session
- func (session *Session) SetHeaderSingle(key, val string) *Session
- func (session *Session) SetProxyHandler(handler func(req *http.Request) (*url.URL, error)) *Session
- func (session *Session) SetProxyURL(u *url.URL) *Session
- func (session *Session) SetProxyUrl(rawurl string) *Session
- func (session *Session) SetTimeout(t time.Duration) *Session
- func (session *Session) SetUserAgent(ua string) *Session
- func (session *Session) Suck(req *Request, ops ...OptionFunc) (*Response, error)
- func (session *Session) Trace(rawurl string, ops ...OptionFunc) (*Response, error)
- func (session *Session) Use(middleware ...HandlerFunc) *Session
- type SessionOptions
Constants ¶
const ( // default redirect num DefaultRedirectNum = 10 // request context redirect num key name ContextRedirectNumKey = "redirectNum" )
const ContextProxyKey = "proxy"
request context proxy key name
Variables ¶
var ( ErrRequestBody = errors.New("request encode can`t coexists with PostForm") ErrTimeout = errors.New("reqeust timeout") )
Functions ¶
func MergeHeaders ¶
mergeHeaders merge Request headers and Session Headers. Request has higher priority.
func MergeQueryString ¶
Get request merge url and query string encode.
func ReplaceQueryString ¶
Get request replace url and query string encode.
Types ¶
type Cookies ¶
defined []http.Cookie alias Cookies
func NewCookiesWithString ¶
You should init it by using NewCookiesWithString like this:
cookies := quick.NewCookiesWithString( "key1=value1; key2=value2; key3=value3" )
Note: param is cookie string
type HandlerFunc ¶
type OptionFunc ¶
type OptionFunc func(*Request)
request option func
func OptionBasicAuth ¶
func OptionBasicAuth(username, password string) OptionFunc
HTTP Basic Authentication
func OptionBodyFormData ¶
func OptionBodyFormData(v interface{}) OptionFunc
request body for post (FormData)
func OptionHeaderSingle ¶
func OptionHeaderSingle(k, v string) OptionFunc
set an http header to request
func OptionQueryString ¶
func OptionQueryString(v interface{}) OptionFunc
request query string for get
type RedirectError ¶
type RedirectError struct {
RedirectNum int
}
func (*RedirectError) Error ¶
func (e *RedirectError) Error() string
type Request ¶
type Request struct {
Id uint64
URL *url.URL
Method string
Header http.Header // request headers
Body io.Reader // request encode
RedirectNum int // Number of redirects requested. default 5
Timeout time.Duration // request timeout
Proxy *url.URL // request proxy url
Cookies Cookies // request cookies
// contains filtered or unexported fields
}
http request payload
func ConvertHttpRequest ¶
Convert http.Request To Request
func NewRequestWithContext ¶
create a request instance with context.Context
func (*Request) BasicAuth ¶
BasicAuth returns the username and password provided in the request's Authorization header, if the request uses HTTP Basic Authentication. See RFC 2617, Section 2.
func (*Request) GetHeaderSingle ¶
get request header single
func (*Request) GetProxyURL ¶
get proxy url.URL for this request
func (*Request) GetProxyUrl ¶
get proxy url for this request
func (*Request) GetTimeout ¶
func (*Request) SetBasicAuth ¶
SetBasicAuth sets the request's Authorization header to use HTTP Basic Authentication with the provided username and password.
With HTTP Basic Authentication the provided username and password are not encrypted.
Some protocols may impose additional requirements on pre-escaping the username and password. For instance, when used with OAuth2, both arguments must be URL encoded first with url.QueryEscape.
func (*Request) SetBodyFormData ¶
set POST body (FormData) to request
func (*Request) SetBodyJson ¶
func (*Request) SetBodyXWwwFormUrlencoded ¶
func (*Request) SetBodyXml ¶
func (*Request) SetCharset ¶
set request charset
func (*Request) SetCookies ¶
set cookies to request sample:
quick.SetCookies(
quick.NewCookiesWithString("key1=value1; key2=value2; key3=value3")
)
func (*Request) SetHeaderSingle ¶
set request header single
func (*Request) SetHeaders ¶
merge request origin header and header
func (*Request) SetProxyURL ¶
set the proxy url for this request
func (*Request) SetProxyUrl ¶
set the proxy for this request eg. "http://127.0.0.1:8080" "http://username:[email protected]:8080"
func (*Request) SetQueryString ¶
set GET parameters to request
func (*Request) SetReferer ¶
set request referer
func (*Request) SetUserAgent ¶
set request user-agent
type Response ¶
type Response struct {
RequestId uint64 // request id
Status string // e.g. "200 OK"
StatusCode int // e.g. 200
Proto string // e.g. "HTTP/1.0"
ProtoMajor int // e.g. 1
ProtoMinor int // e.g. 0
Header http.Header
Body *bytes.Buffer
ContentLength int64
ExecTime time.Duration // request exec time
TLS *tls.ConnectionState
TransferEncoding []string
Encoding encoding.Encoding // Response body encoding
}
func PostFormData ¶
func PostFormData(rawurl string, ops ...OptionFunc) (*Response, error)
postForm request
func (*Response) GetContextType ¶
func (*Response) GetHeaderSingle ¶
type Session ¶
type Session struct {
Header http.Header
Proxy *url.URL
Timeout time.Duration
// contains filtered or unexported fields
}
func NewSession ¶
func NewSession(options ...*SessionOptions) *Session
func SetCheckRedirectHandler ¶
set global checkRedirect handler handler: func(req *http.Request, via []*http.Request) error
func SetProxyHandler ¶
set global proxy handler handler: func(req *http.Request) (*url.URL, error)
func SetTimeout ¶
set global request timeout example: time.Second * 30
func (*Session) Connect ¶
func (session *Session) Connect(rawurl string, ops ...OptionFunc) (*Response, error)
connect request
func (*Session) Delete ¶
func (session *Session) Delete(rawurl string, ops ...OptionFunc) (*Response, error)
delete request
func (*Session) Get ¶
func (session *Session) Get(rawurl string, ops ...OptionFunc) (*Response, error)
get request
func (*Session) GetHeaderSingle ¶
get session global header single
func (*Session) GetProxyURL ¶
get session global proxy url
func (*Session) GetProxyUrl ¶
get session global proxy url
func (*Session) GetUserAgent ¶
get session global user-agent
func (*Session) Head ¶
func (session *Session) Head(rawurl string, ops ...OptionFunc) (*Response, error)
head request
func (*Session) InsecureSkipVerify ¶
ssl skip verify
func (*Session) Options ¶
func (session *Session) Options(rawurl string, ops ...OptionFunc) (*Response, error)
options request
func (*Session) Patch ¶
func (session *Session) Patch(rawurl string, ops ...OptionFunc) (*Response, error)
patch request
func (*Session) Post ¶
func (session *Session) Post(rawurl string, ops ...OptionFunc) (*Response, error)
post request
func (*Session) PostFormData ¶
func (session *Session) PostFormData(rawurl string, ops ...OptionFunc) (*Response, error)
postForm request
func (*Session) Put ¶
func (session *Session) Put(rawurl string, ops ...OptionFunc) (*Response, error)
put request
func (*Session) SetCheckRedirectHandler ¶
func (session *Session) SetCheckRedirectHandler(handler func(req *http.Request, via []*http.Request) error) *Session
set session global checkRedirect handler handler: func(req *http.Request, via []*http.Request) error
func (*Session) SetCookieJar ¶
set session global cookieJar
func (*Session) SetCookies ¶
set cookies of the url in Session.
func (*Session) SetHeaderSingle ¶
set session global header single
func (*Session) SetProxyHandler ¶
set session global proxy handler handler: func(req *http.Request) (*url.URL, error)
func (*Session) SetProxyURL ¶
set session global proxy url
func (*Session) SetProxyUrl ¶
set session global proxy url
func (*Session) SetTimeout ¶
set session global request timeout example: time.Second * 30
func (*Session) SetUserAgent ¶
set session global user-agent
func (*Session) Suck ¶
func (session *Session) Suck(req *Request, ops ...OptionFunc) (*Response, error)
request suck data
func (*Session) Trace ¶
func (session *Session) Trace(rawurl string, ops ...OptionFunc) (*Response, error)
trace request
func (*Session) Use ¶
func (session *Session) Use(middleware ...HandlerFunc) *Session
use middleware handler
type SessionOptions ¶
type SessionOptions struct {
// DialTimeout is the maximum amount of time a dial will wait for
// a connect to complete.
//
// When using TCP and dialing a host name with multiple IP
// addresses, the timeout may be divided between them.
//
// With or without a timeout, the operating system may impose
// its own earlier timeout. For instance, TCP timeouts are
// often around 3 minutes.
DialTimeout time.Duration
// DialKeepAlive specifies the interval between keep-alive
// probes for an active network connection.
//
// Network protocols or operating systems that do
// not support keep-alives ignore this field.
// If negative, keep-alive probes are disabled.
DialKeepAlive time.Duration
// MaxConnsPerHost optionally limits the total number of
// connections per host, including connections in the dialing,
// active, and idle states. On limit violation, dials will block.
//
// Zero means no limit.
MaxConnsPerHost int
// MaxIdleConns controls the maximum number of idle (keep-alive)
// connections across all hosts. Zero means no limit.
MaxIdleConns int
// MaxIdleConnsPerHost, if non-zero, controls the maximum idle
// (keep-alive) connections to keep per-host. If zero,
// DefaultMaxIdleConnsPerHost is used.
MaxIdleConnsPerHost int
// IdleConnTimeout is the maximum amount of time an idle
// (keep-alive) connection will remain idle before closing
// itself.
// Zero means no limit.
IdleConnTimeout time.Duration
// TLSHandshakeTimeout specifies the maximum amount of time waiting to
// wait for a TLS handshake. Zero means no timeout.
TLSHandshakeTimeout time.Duration
// ExpectContinueTimeout, if non-zero, specifies the amount of
// time to wait for a server's first response headers after fully
// writing the request headers if the request has an
// "Expect: 100-continue" header. Zero means no timeout and
// causes the encode to be sent immediately, without
// waiting for the server to approve.
// This time does not include the time to send the request header.
ExpectContinueTimeout time.Duration
// DisableCookieJar specifies whether disable session cookiejar.
DisableCookieJar bool
// DisableDialKeepAlives, if true, disables HTTP keep-alives and
// will only use the connection to the server for a single
// HTTP request.
//
// This is unrelated to the similarly named TCP keep-alives.
DisableDialKeepAlives bool
}
func DefaultSessionOptions ¶
func DefaultSessionOptions() *SessionOptions
DefaultSessionOptions return a default SessionOptions object.