requests

package module
v0.0.0-...-d857d7c Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2020 License: MIT Imports: 23 Imported by: 0

README

requests

Http request library for Go

Documentation

Index

Constants

View Source
const ContextProxyKey = "proxy"

request context proxy key name

View Source
const ContextRedirectNumKey = "redirectNum"

request context redirect num key name

View Source
const UA = "Go library telanflow/requests"

Variables

View Source
var (
	ErrRequestBody = errors.New("request encode can`t coexists with PostForm")
	ErrTimeout     = errors.New("reqeust timeout")
)

Functions

func CopyHeader

func CopyHeader(h http.Header) http.Header

copy headers

func Download

func Download(rawurl string, toFile string) error

download file to save hard disk

func GetHeaderSingle

func GetHeaderSingle(key string) string

get global header single

func GetUserAgent

func GetUserAgent() string

get global user-agent

func MergeHeaders

func MergeHeaders(h1, h2 http.Header) http.Header

mergeHeaders merge Request headers and Session Headers. Request has higher priority.

func MergeQueryParams

func MergeQueryParams(parsedURL *url.URL, parsedQuery string) (*url.URL, error)

Get request merge url and query string encode.

func NewCookieJar

func NewCookieJar() (http.CookieJar, error)

create a cookieJar

func WrapErr

func WrapErr(err error, msg string) error

WrapErr will wrap a error with some information: filename, line, time and some message.

func WrapErrf

func WrapErrf(err error, format string, args ...interface{}) error

WrapErr will wrap a error with some information: filename, line, time and some message. You can format message of error.

Types

type Cookies

type Cookies = []*http.Cookie

defined []http.Cookie alias Cookies

func NewCookiesWithString

func NewCookiesWithString(rawstr string) Cookies

You should init it by using NewCookiesWithString like this:

cookies := requests.NewCookiesWithString(
	"key1=value1; key2=value2; key3=value3"
)

Note: param is cookie string

type Error

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

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

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

func NewRequest(method, rawurl string) (*Request, error)

create a request instance

func (*Request) GetHeader

func (req *Request) GetHeader() http.Header

get request header

func (*Request) GetHeaderSingle

func (req *Request) GetHeaderSingle(key string) string

get request header single

func (*Request) GetUrl

func (req *Request) GetUrl() string

get request url

func (*Request) GetUserAgent

func (req *Request) GetUserAgent() string

get request user-agent

func (*Request) SetBody

func (req *Request) SetBody(params interface{}) *Request

func (*Request) SetBodyFormData

func (req *Request) SetBodyFormData(params interface{}) *Request

func (*Request) SetBodyJson

func (req *Request) SetBodyJson(params interface{}) *Request

func (*Request) SetBodyXWwwFormUrlencoded

func (req *Request) SetBodyXWwwFormUrlencoded(params interface{}) *Request

func (*Request) SetBodyXml

func (req *Request) SetBodyXml(params interface{}) *Request

func (*Request) SetCharset

func (req *Request) SetCharset(charset string) *Request

set request charset

func (*Request) SetCookies

func (req *Request) SetCookies(cookies Cookies) *Request

set cookies to request sample:

requests.SetCookies(
	requests.NewCookiesWithString("key1=value1; key2=value2; key3=value3")
)

func (*Request) SetHeader

func (req *Request) SetHeader(header http.Header) *Request

set request header

func (*Request) SetHeaderSingle

func (req *Request) SetHeaderSingle(key, val string) *Request

set request header single

func (*Request) SetHeaders

func (req *Request) SetHeaders(header http.Header) *Request

merge request origin header and header

func (*Request) SetHost

func (req *Request) SetHost(host string) *Request

Custom request host field GET /index HTTP/1.1 Host: domain ....

func (*Request) SetProxy

func (req *Request) SetProxy(rawurl string) *Request

set the proxy for this request example: http://127.0.0.1:8080

func (*Request) SetQueryString

func (req *Request) SetQueryString(params interface{}) *Request

func (*Request) SetReferer

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

set request referer

func (*Request) SetUrl

func (req *Request) SetUrl(rawurl string) *Request

set request url

func (*Request) SetUserAgent

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

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 BuildResponse

func BuildResponse(resp *http.Response) (*Response, error)

func Connect

func Connect(rawurl string, params ...interface{}) (*Response, error)

connect request

func Delete

func Delete(rawurl string, params ...interface{}) (*Response, error)

delete request

func Get

func Get(rawurl string, params ...interface{}) (*Response, error)

get request

func Head(rawurl string, params ...interface{}) (*Response, error)

head request

func Options

func Options(rawurl string, params ...interface{}) (*Response, error)

options request

func Patch

func Patch(rawurl string, params ...interface{}) (*Response, error)

patch request

func Post

func Post(rawurl string, params ...interface{}) (*Response, error)

post request

func PostFormData

func PostFormData(rawurl string, params ...interface{}) (*Response, error)

postForm request

func Put

func Put(rawurl string, params ...interface{}) (*Response, error)

put request

func Suck

func Suck(req *Request) (*Response, error)

request suck data

func Trace

func Trace(rawurl string, params ...interface{}) (*Response, error)

trace request

func (*Response) GetBody

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

func (*Response) GetContextType

func (r *Response) GetContextType() string

func (*Response) GetHeader

func (r *Response) GetHeader() http.Header

func (*Response) GetHeaderSingle

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

func (*Response) GetHtml

func (r *Response) GetHtml() string

func (*Response) GetJson

func (r *Response) GetJson(v interface{}) error

func (*Response) GetXml

func (r *Response) GetXml(v interface{}) error

func (*Response) Read

func (r *Response) Read(p []byte) (n int, err error)

func (*Response) String

func (r *Response) String() string

type Session

type Session struct {
	Header  http.Header
	Proxy   *url.URL
	Timeout time.Duration
	// contains filtered or unexported fields
}

func InsecureSkipVerify

func InsecureSkipVerify(skip bool) *Session

ssl skip verify

func NewSession

func NewSession(options ...*SessionOptions) *Session

func SetCheckRedirectHandler

func SetCheckRedirectHandler(handler func(req *http.Request, via []*http.Request) error) *Session

set global checkRedirect handler handler: func(req *http.Request, via []*http.Request) error

func SetCookieJar

func SetCookieJar(jar http.CookieJar) *Session

set global cookieJar

func SetHeaderSingle

func SetHeaderSingle(key, val string) *Session

set global header single

func SetProxy

func SetProxy(rawurl string) *Session

set global proxy url

func SetProxyHandler

func SetProxyHandler(handler func(req *http.Request) (*url.URL, error)) *Session

set global proxy handler handler: func(req *http.Request) (*url.URL, error)

func SetTimeout

func SetTimeout(t time.Duration) *Session

set global request timeout example: time.Second * 30

func SetUserAgent

func SetUserAgent(ua string) *Session

set global user-agent

func (*Session) Connect

func (session *Session) Connect(rawurl string, params ...interface{}) (*Response, error)

connect request

func (*Session) Delete

func (session *Session) Delete(rawurl string, params ...interface{}) (*Response, error)

delete request

func (*Session) Download

func (session *Session) Download(rawurl string, toFile string) error

download file

func (*Session) Get

func (session *Session) Get(rawurl string, params ...interface{}) (*Response, error)

get request

func (*Session) GetHeaderSingle

func (session *Session) GetHeaderSingle(key string) string

get session global header single

func (*Session) GetUserAgent

func (session *Session) GetUserAgent() string

get session global user-agent

func (*Session) Head

func (session *Session) Head(rawurl string, params ...interface{}) (*Response, error)

head request

func (*Session) InsecureSkipVerify

func (session *Session) InsecureSkipVerify(skip bool) *Session

ssl skip verify

func (*Session) Options

func (session *Session) Options(rawurl string, params ...interface{}) (*Response, error)

options request

func (*Session) Patch

func (session *Session) Patch(rawurl string, params ...interface{}) (*Response, error)

patch request

func (*Session) Post

func (session *Session) Post(rawurl string, params ...interface{}) (*Response, error)

post request

func (*Session) PostFormData

func (session *Session) PostFormData(rawurl string, params ...interface{}) (*Response, error)

postForm request

func (*Session) Put

func (session *Session) Put(rawurl string, params ...interface{}) (*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

func (session *Session) SetCookieJar(jar http.CookieJar) *Session

set session global cookieJar

func (*Session) SetHeaderSingle

func (session *Session) SetHeaderSingle(key, val string) *Session

set session global header single

func (*Session) SetProxy

func (session *Session) SetProxy(rawurl string) *Session

set session global proxy url

func (*Session) SetProxyHandler

func (session *Session) SetProxyHandler(handler func(req *http.Request) (*url.URL, error)) *Session

set session global proxy handler handler: func(req *http.Request) (*url.URL, error)

func (*Session) SetTimeout

func (session *Session) SetTimeout(t time.Duration) *Session

set session global request timeout example: time.Second * 30

func (*Session) SetUserAgent

func (session *Session) SetUserAgent(ua string) *Session

set session global user-agent

func (*Session) Suck

func (session *Session) Suck(req *Request) (*Response, error)

request suck data

func (*Session) Trace

func (session *Session) Trace(rawurl string, params ...interface{}) (*Response, error)

trace request

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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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