Documentation
¶
Index ¶
- Constants
- Variables
- func CopyHeader(h http.Header) http.Header
- func Download(rawurl string, toFile string) error
- func GetHeaderSingle(key string) string
- func GetUserAgent() string
- func MergeHeaders(h1, h2 http.Header) http.Header
- func MergeQueryParams(parsedURL *url.URL, parsedQuery string) (*url.URL, error)
- func NewCookieJar() (http.CookieJar, error)
- func WrapErr(err error, msg string) error
- func WrapErrf(err error, format string, args ...interface{}) error
- type Cookies
- type Error
- type RedirectError
- type Request
- func (req *Request) GetHeader() http.Header
- func (req *Request) GetHeaderSingle(key string) string
- func (req *Request) GetUrl() string
- func (req *Request) GetUserAgent() 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) SetProxy(rawurl string) *Request
- func (req *Request) SetQueryString(params interface{}) *Request
- func (req *Request) SetReferer(referer string) *Request
- func (req *Request) SetUrl(rawurl string) *Request
- func (req *Request) SetUserAgent(ua string) *Request
- type Response
- func BuildResponse(resp *http.Response) (*Response, error)
- func Connect(rawurl string, params ...interface{}) (*Response, error)
- func Delete(rawurl string, params ...interface{}) (*Response, error)
- func Get(rawurl string, params ...interface{}) (*Response, error)
- func Head(rawurl string, params ...interface{}) (*Response, error)
- func Options(rawurl string, params ...interface{}) (*Response, error)
- func Patch(rawurl string, params ...interface{}) (*Response, error)
- func Post(rawurl string, params ...interface{}) (*Response, error)
- func PostFormData(rawurl string, params ...interface{}) (*Response, error)
- func Put(rawurl string, params ...interface{}) (*Response, error)
- func Suck(req *Request) (*Response, error)
- func Trace(rawurl string, params ...interface{}) (*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 SetHeaderSingle(key, val string) *Session
- func SetProxy(rawurl string) *Session
- func SetProxyHandler(handler func(req *http.Request) (*url.URL, error)) *Session
- func SetTimeout(t time.Duration) *Session
- func SetUserAgent(ua string) *Session
- func (session *Session) Connect(rawurl string, params ...interface{}) (*Response, error)
- func (session *Session) Delete(rawurl string, params ...interface{}) (*Response, error)
- func (session *Session) Download(rawurl string, toFile string) error
- func (session *Session) Get(rawurl string, params ...interface{}) (*Response, error)
- func (session *Session) GetHeaderSingle(key string) string
- func (session *Session) GetUserAgent() string
- func (session *Session) Head(rawurl string, params ...interface{}) (*Response, error)
- func (session *Session) InsecureSkipVerify(skip bool) *Session
- func (session *Session) Options(rawurl string, params ...interface{}) (*Response, error)
- func (session *Session) Patch(rawurl string, params ...interface{}) (*Response, error)
- func (session *Session) Post(rawurl string, params ...interface{}) (*Response, error)
- func (session *Session) PostFormData(rawurl string, params ...interface{}) (*Response, error)
- func (session *Session) Put(rawurl string, params ...interface{}) (*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) SetHeaderSingle(key, val string) *Session
- func (session *Session) SetProxy(rawurl string) *Session
- func (session *Session) SetProxyHandler(handler func(req *http.Request) (*url.URL, error)) *Session
- func (session *Session) SetTimeout(t time.Duration) *Session
- func (session *Session) SetUserAgent(ua string) *Session
- func (session *Session) Suck(req *Request) (*Response, error)
- func (session *Session) Trace(rawurl string, params ...interface{}) (*Response, error)
- type SessionOptions
Constants ¶
const ContextProxyKey = "proxy"
request context proxy key name
const ContextRedirectNumKey = "redirectNum"
request context redirect num key name
const UA = "Go library telanflow/requests"
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 MergeQueryParams ¶
Get request merge 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 := requests.NewCookiesWithString( "key1=value1; key2=value2; key3=value3" )
Note: param is cookie string
type RedirectError ¶
type RedirectError struct {
RedirectNum int
}
func (*RedirectError) Error ¶
func (e *RedirectError) Error() string
type Request ¶
type Request struct {
Id uint64
Url string
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 NewRequest ¶
create a request instance
func (*Request) GetHeaderSingle ¶
get request header single
func (*Request) SetBodyFormData ¶
func (*Request) SetBodyJson ¶
func (*Request) SetBodyXWwwFormUrlencoded ¶
func (*Request) SetBodyXml ¶
func (*Request) SetCharset ¶
set request charset
func (*Request) SetCookies ¶
set cookies to request sample:
requests.SetCookies(
requests.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) SetProxy ¶
set the proxy for this request example: http://127.0.0.1:8080
func (*Request) SetQueryString ¶
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 []byte
ContentLength int64
ExecTime time.Duration // request exec time
// contains filtered or unexported fields
}
func PostFormData ¶
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) GetHeaderSingle ¶
get session global header single
func (*Session) GetUserAgent ¶
get session global user-agent
func (*Session) InsecureSkipVerify ¶
ssl skip verify
func (*Session) PostFormData ¶
postForm 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) SetHeaderSingle ¶
set session global header single
func (*Session) SetProxyHandler ¶
set session global proxy handler handler: func(req *http.Request) (*url.URL, error)
func (*Session) SetTimeout ¶
set session global request timeout example: time.Second * 30
func (*Session) SetUserAgent ¶
set session global user-agent
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.