requests

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2021 License: MIT Imports: 36 Imported by: 6

README

HTTP Requests for Humans

Go Report Card GoDoc GitHub license

Install

go get -u -v github.com/wzshiming/requests

Usage

API Documentation

Example

License

Pouch is licensed under the MIT License. See LICENSE for the full license text.

Documentation

Index

Constants

View Source
const (
	// LogIgnore is ignore all information
	LogIgnore logLevel = iota
	// LogError is show Errors Only
	LogError
	// LogInfo is show basic information
	LogInfo
	// LogMessageHead is show header information
	LogMessageHead
	// LogMessageAll is show all information
	LogMessageAll
)
View Source
const (
	MethodGet     = "GET"
	MethodHead    = "HEAD"
	MethodPost    = "POST"
	MethodPut     = "PUT"
	MethodPatch   = "PATCH" // RFC 5789
	MethodDelete  = "DELETE"
	MethodConnect = "CONNECT"
	MethodOptions = "OPTIONS"
	MethodTrace   = "TRACE"

	MimeJSON        = "application/json" + charsetUTF8
	MimeXML         = "application/xml" + charsetUTF8
	MimeTextPlain   = "text/plain" + charsetUTF8
	MimeOctetStream = "application/octet-stream" + charsetUTF8
	MimeURLEncoded  = "application/x-www-form-urlencoded" + charsetUTF8
	MimeFormData    = "multipart/form-data" + charsetUTF8

	HeaderUserAgent       = "User-Agent"
	HeaderAccept          = "Accept"
	HeaderContentType     = "Content-Type"
	HeaderContentLength   = "Content-Length"
	HeaderContentEncoding = "Content-Encoding"
	HeaderAuthorization   = "Authorization"
)

Common HTTP methods.

Unless otherwise noted, these are defined in RFC 7231 section 4.3.

Variables

View Source
var (
	ErrNoRedirect   = errors.New("No Redirect")
	ErrNotTransport = errors.New("not a *http.Transport")
)
View Source
var (
	DefaultPrefix         = "REQUESTS"
	DefaultVersion        = "1.0"
	DefaultUserAgentValue = "Mozilla/5.0 (compatible; " + DefaultPrefix + "/" + DefaultVersion + "; +https://github.com/wzshiming/requests)"
)

Default

View Source
var ErrNotExist = errors.New("not exist")

Functions

func Cookies

func Cookies(raw interface{}) []*http.Cookie

Cookies raw to Cookies.

func FileCacheDir

func FileCacheDir(s string) fileCacheDir

func MarshalRequest added in v0.0.2

func MarshalRequest(req *http.Request) ([]byte, error)

MarshalRequest returns text of the request

func MarshalResponse added in v0.0.2

func MarshalResponse(resp *http.Response) ([]byte, error)

MarshalRequest returns text of the request

func MemoryCache

func MemoryCache() memoryCacheDir

func RequestHash added in v0.1.0

func RequestHash(r *http.Request) (string, error)

func TryCharset

func TryCharset(r io.Reader, contentType string) (io.Reader, string, error)

TryCharset try charset

func TryHTMLCharset added in v0.0.2

func TryHTMLCharset(r io.Reader) (io.Reader, string, error)

TryHTMLCharset try html charset

func URL

func URL(raw interface{}) *url.URL

URL raw to URL structure.

func UnmarshalRequest added in v0.0.2

func UnmarshalRequest(data []byte) (req *http.Request, err error)

UnmarshalRequest reads and returns an HTTP request from data.

func UnmarshalResponse added in v0.0.2

func UnmarshalResponse(data []byte) (resp *http.Response, err error)

UnmarshalResponse reads and returns an HTTP response from data.

Types

type Cache

type Cache interface {
	Hash(*Request) (string, error)
	Load(name string) (*Response, error)
	Save(name string, resp *Response) error
	Del(name string) error
}

type Client

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

Client contains basic

func NewClient

func NewClient() *Client

NewClient the create a client

func (*Client) AddCookies

func (c *Client) AddCookies(u *url.URL, cookies []*http.Cookie) *Client

AddCookies adds cookie to the client.

func (*Client) AddRootCert

func (c *Client) AddRootCert(cert *x509.Certificate) *Client

AddRootCert adds one or more root certificates into requests client

func (*Client) GetCookie

func (c *Client) GetCookie(u *url.URL, name string) *http.Cookie

GetCookie gets cookie.

func (*Client) GetCookieJar

func (c *Client) GetCookieJar() http.CookieJar

GetCookieJar gets cookie jar.

func (*Client) NewRequest

func (c *Client) NewRequest() *Request

NewRequest creates a request instance.

func (*Client) NoRedirect

func (c *Client) NoRedirect() *Client

NoRedirect disable redirects

func (*Client) Process

func (c *Client) Process(req *http.Request) (*http.Response, error)

Process executes and returns response

func (*Client) SetCache

func (c *Client) SetCache(cache Cache) *Client

SetCache sets cache

func (*Client) SetCheckRedirect

func (c *Client) SetCheckRedirect(f func(req *http.Request, via []*http.Request) error) *Client

SetCheckRedirect sets specifies the policy for handling redirects

func (*Client) SetCookieJar

func (c *Client) SetCookieJar(jar http.CookieJar) *Client

SetCookieJar sets cookie jar.

func (*Client) SetDial

func (c *Client) SetDial(dial func(network, addr string) (net.Conn, error)) *Client

SetDial sets Dial.

func (*Client) SetDialContext

func (c *Client) SetDialContext(dial func(ctx context.Context, network, addr string) (net.Conn, error)) *Client

SetDialContext sets DialContext.

func (*Client) SetDialTLS

func (c *Client) SetDialTLS(dial func(network, addr string) (net.Conn, error)) *Client

SetDialTLS sets DialTLS.

func (*Client) SetKeepAlives

func (c *Client) SetKeepAlives(enable bool) *Client

SetKeepAlives sets the keep alives.

func (*Client) SetLogLevel

func (c *Client) SetLogLevel(l logLevel) *Client

SetLogLevel sets log level.

func (*Client) SetLogger

func (c *Client) SetLogger(w io.Writer) *Client

SetLogger sets given writer for logging.

func (*Client) SetProxyFromEnvironment

func (c *Client) SetProxyFromEnvironment(u bool) *Client

SetProxyFromEnvironment sets the Proxy URL.

func (*Client) SetProxyFunc

func (c *Client) SetProxyFunc(proxy func(*http.Request) (*url.URL, error)) *Client

SetProxyFunc sets the Proxy function.

func (*Client) SetProxyURL

func (c *Client) SetProxyURL(u *url.URL) *Client

SetProxyURL sets the Proxy URL.

func (*Client) SetProxyURLByStr

func (c *Client) SetProxyURLByStr(rawurl string) *Client

SetProxyURLByStr sets the Proxy URL.

func (*Client) SetSkipVerify

func (c *Client) SetSkipVerify(b bool) *Client

SetSkipVerify sets skip ca verify

func (*Client) SetTLSClientConfig

func (c *Client) SetTLSClientConfig(config *tls.Config) *Client

SetTLSClientConfig sets TLSClientConfig.

func (*Client) SetTimeout

func (c *Client) SetTimeout(timeout time.Duration) *Client

SetTimeout sets timeout for request raised from client.

func (*Client) WithCookieJar

func (c *Client) WithCookieJar() *Client

WithCookieJar with default cookie jar.

func (*Client) WithLogger

func (c *Client) WithLogger() *Client

WithLogger with logger.

func (*Client) WithSystemCertPool

func (c *Client) WithSystemCertPool() *Client

WithSystemCertPool sets system cert poll

type Request

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

Request type is used to compose and send individual request from client

func NewRequest

func NewRequest() *Request

NewRequest returns a default request

func (*Request) AddCookies

func (r *Request) AddCookies(cookies []*http.Cookie) *Request

AddCookies adds cookie to the client.

func (*Request) AddForm

func (r *Request) AddForm(param, value string) *Request

AddForm adds from field and its value in the current request.

func (*Request) AddFormIfNot

func (r *Request) AddFormIfNot(param, value string) *Request

AddFormIfNot adds from field and its value in the current request if not.

func (*Request) AddForms

func (r *Request) AddForms(param string, value []string) *Request

AddForm adds froms field and its value in the current request.

func (*Request) AddHeader

func (r *Request) AddHeader(param, value string) *Request

AddHeader adds header field and its value in the current request.

func (*Request) AddHeaderIfNot

func (r *Request) AddHeaderIfNot(param, value string) *Request

AddHeaderIfNot adds header field and its value in the current request if not.

func (*Request) AddHeaders

func (r *Request) AddHeaders(param string, value []string) *Request

AddHeaders adds header field and its value in the current request.

func (*Request) AddPathIfNot

func (r *Request) AddPathIfNot(param, value string) *Request

AddPathIfNot adds path parameter and its value in the current request if not.

func (*Request) AddQuery

func (r *Request) AddQuery(param, value string) *Request

AddQuery adds query field and its value in the current request.

func (*Request) AddQueryIfNot

func (r *Request) AddQueryIfNot(param, value string) *Request

AddQueryIfNot adds query field and its value in the current request if not.

func (*Request) AddQuerys

func (r *Request) AddQuerys(param string, value []string) *Request

AddQuerys adds query field and its value in the current request.

func (*Request) Client

func (r *Request) Client() *Client

Client returns the client of the request

func (*Request) Clone

func (r *Request) Clone() *Request

Clone returns clone the request

func (*Request) Delete

func (r *Request) Delete(url string) (*Response, error)

Delete does DELETE HTTP request.

func (*Request) Do

func (r *Request) Do() (*Response, error)

Do performs the HTTP request

func (*Request) Get

func (r *Request) Get(url string) (*Response, error)

Get does GET HTTP request.

func (*Request) GetURL

func (r *Request) GetURL(rawurl string) *url.URL

GetURL gets URL.

func (*Request) Head

func (r *Request) Head(url string) (*Response, error)

Head does HEAD HTTP request.

func (*Request) Message

func (r *Request) Message() string

Message returns the HTTP request all information

func (*Request) MessageHead

func (r *Request) MessageHead() string

MessageHead returns the HTTP request header information

func (*Request) NoCache

func (r *Request) NoCache() *Request

NoCache Clear the cache for this request

func (*Request) Options

func (r *Request) Options(url string) (*Response, error)

Options does OPTIONS HTTP request.

func (*Request) Patch

func (r *Request) Patch(url string) (*Response, error)

Patch does PATCH HTTP request.

func (*Request) Post

func (r *Request) Post(url string) (*Response, error)

Post does POST HTTP request.

func (*Request) Put

func (r *Request) Put(url string) (*Response, error)

Put does PUT HTTP request.

func (*Request) RawRequest

func (r *Request) RawRequest() (*http.Request, error)

func (*Request) SetAuthToken

func (r *Request) SetAuthToken(token string) *Request

SetAuthToken sets bearer auth token header in the HTTP request.

func (*Request) SetBasicAuth

func (r *Request) SetBasicAuth(username, password string) *Request

SetBasicAuth sets basic authentication header in the HTTP request.

func (*Request) SetBody

func (r *Request) SetBody(body io.Reader) *Request

SetBody sets request body for the request.

func (*Request) SetCharset added in v0.0.2

func (r *Request) SetCharset(transformer transform.Transformer) *Request

func (*Request) SetCharsetWithStr added in v0.0.2

func (r *Request) SetCharsetWithStr(cs string) *Request

func (*Request) SetContentType

func (r *Request) SetContentType(contentType string) *Request

SetContentType sets content type header in the HTTP request.

func (*Request) SetContext

func (r *Request) SetContext(ctx context.Context) *Request

SetContext sets context.Context for current Request.

func (*Request) SetDeadline

func (r *Request) SetDeadline(d time.Time) *Request

SetDeadline sets deadline for current Request.

func (*Request) SetDiscardResponse

func (r *Request) SetDiscardResponse(discard bool) *Request

SetDiscardResponse sets unread the response body.

func (*Request) SetFile

func (r *Request) SetFile(param, fileName, contentType string, reader io.Reader) *Request

SetFile sets custom data using io.Reader for multipart upload.

func (*Request) SetForm

func (r *Request) SetForm(param, value string) *Request

SetForm sets multiple form parameters with multi-value

func (*Request) SetHeader

func (r *Request) SetHeader(param, value string) *Request

SetHeader sets header field and its value in the current request.

func (*Request) SetJSON

func (r *Request) SetJSON(i interface{}) *Request

SetJSON sets data encoded by JSON to the request body.

func (*Request) SetMethod

func (r *Request) SetMethod(method string) *Request

SetMethod sets method in the HTTP request.

func (*Request) SetPath

func (r *Request) SetPath(param, value string) *Request

SetPath sets path parameter and its value in the current request.

func (*Request) SetQuery

func (r *Request) SetQuery(param, value string) *Request

SetQuery sets query parameter and its value in the current request.

func (*Request) SetTimeout

func (r *Request) SetTimeout(timeout time.Duration) *Request

SetTimeout sets timeout for current Request.

func (*Request) SetURL

func (r *Request) SetURL(u *url.URL) *Request

SetURL sets URL in the client instance.

func (*Request) SetURLByStr

func (r *Request) SetURLByStr(rawurl string) *Request

SetURLByStr sets URL in the client instance.

func (*Request) SetUserAgent

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

SetUserAgent sets user agent header in the HTTP request.

func (*Request) SetXML

func (r *Request) SetXML(i interface{}) *Request

SetXML sets data encoded by XML to the request body.

func (*Request) String

func (r *Request) String() string

String returns the HTTP request basic information

func (*Request) Trace

func (r *Request) Trace(url string) (*Response, error)

Trace does TRACE HTTP request.

type Response

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

Response is an object represents executed request and its values.

func (*Response) Body

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

Body returns HTTP response as []byte array for the executed request.

func (*Response) ContentType

func (r *Response) ContentType() string

ContentType returns HTTP response content type

func (*Response) Cookies

func (r *Response) Cookies() []*http.Cookie

Cookies to access all the response cookies

func (*Response) Header

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

Header returns the response headers

func (*Response) Location

func (r *Response) Location() *url.URL

Location return request url.

func (*Response) MarshalText added in v0.0.2

func (r *Response) MarshalText() ([]byte, error)

func (*Response) Message

func (r *Response) Message() string

Message returns the HTTP response all information

func (*Response) MessageHead

func (r *Response) MessageHead() string

MessageHead returns the HTTP response header information

func (*Response) RawBody

func (r *Response) RawBody() io.Reader

RawBody returns the HTTP raw response body.

func (*Response) RawResponse

func (r *Response) RawResponse() *http.Response

func (*Response) RecvAt added in v0.0.2

func (r *Response) RecvAt() time.Time

RecvAt returns when response got recv from server for the request.

func (*Response) SendAt added in v0.0.2

func (r *Response) SendAt() time.Time

SendAt returns when response got send from server for the request.

func (*Response) Size

func (r *Response) Size() int

Size returns the HTTP response size in bytes.

func (*Response) Status

func (r *Response) Status() string

Status returns the HTTP status string for the executed request.

func (*Response) StatusCode

func (r *Response) StatusCode() int

StatusCode returns the HTTP status code for the executed request.

func (*Response) String

func (r *Response) String() string

String returns the HTTP response basic information

func (*Response) Time

func (r *Response) Time() time.Duration

Time returns the time of HTTP response time that from request we sent and received a request.

func (*Response) UnarshalText added in v0.0.2

func (r *Response) UnarshalText(data []byte) error

func (*Response) WriteFile

func (r *Response) WriteFile(file string) error

WriteFile is writes the response body to file.

Jump to

Keyboard shortcuts

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