Documentation ¶
Index ¶
- Constants
- Variables
- func NewRequest(method, url string, opts ...RequestOption) (*http.Request, error)
- func NewRequestWithContext(ctx context.Context, method, url string, opts ...RequestOption) (*http.Request, error)
- type RequestOption
- func Authorization(a string) RequestOption
- func BasicAuth(username, password string) RequestOption
- func Body(r io.Reader) RequestOption
- func ContentType(ct string) RequestOption
- func Cookie(k, v string) RequestOption
- func Cookies(m map[string]string, replace ...bool) RequestOption
- func Deadline(t time.Time) RequestOption
- func File(fieldName string, file *os.File) RequestOption
- func FileFromReader(fieldName, fileName string, r io.Reader) RequestOption
- func Form(m map[string]string) RequestOption
- func Header(k string, v interface{}) RequestOption
- func Headers(m map[string]interface{}, replace ...bool) RequestOption
- func JSON(i interface{}) RequestOption
- func MultipartField(fieldName string, r io.Reader) RequestOption
- func MultipartFieldBytes(fieldName string, value []byte) RequestOption
- func MultipartFieldString(fieldName, value string) RequestOption
- func Queries(m map[string]string, replace ...bool) RequestOption
- func QueriesFromValue(v url.Values) RequestOption
- func Query(k, v string) RequestOption
- func Referer(r string) RequestOption
- func Timeout(d time.Duration) RequestOption
- func UserAgent(ua string) RequestOption
- type RequestOptions
- type Response
- func Connect(url string, opts ...RequestOption) (*Response, error)
- func Delete(url string, opts ...RequestOption) (*Response, error)
- func Get(url string, opts ...RequestOption) (*Response, error)
- func Head(url string, opts ...RequestOption) (*Response, error)
- func NewResponse(r *http.Response) *Response
- func Options(url string, opts ...RequestOption) (*Response, error)
- func Patch(url string, opts ...RequestOption) (*Response, error)
- func Post(url string, opts ...RequestOption) (*Response, error)
- func Put(url string, opts ...RequestOption) (*Response, error)
- func Request(method, url string, opts ...RequestOption) (*Response, error)
- func Trace(url string, opts ...RequestOption) (*Response, error)
- type Session
- func (s *Session) Connect(url string, opts ...RequestOption) (*Response, error)
- func (s *Session) Delete(url string, opts ...RequestOption) (*Response, error)
- func (s *Session) Do(r *http.Request) (*Response, error)
- func (s *Session) Get(url string, opts ...RequestOption) (*Response, error)
- func (s *Session) Head(url string, opts ...RequestOption) (*Response, error)
- func (s *Session) Options(url string, opts ...RequestOption) (*Response, error)
- func (s *Session) Patch(url string, opts ...RequestOption) (*Response, error)
- func (s *Session) Post(url string, opts ...RequestOption) (*Response, error)
- func (s *Session) Put(url string, opts ...RequestOption) (*Response, error)
- func (s *Session) Request(method, url string, opts ...RequestOption) (*Response, error)
- func (s *Session) Trace(url string, opts ...RequestOption) (*Response, error)
Constants ¶
const ( HeaderContentType = "Content-Type" HeaderUserAgent = "User-Agent" HeaderReferer = "Referer" HeaderAuthorization = "Authorization" )
Common header.
const ( ContentTypeJSON = "application/json" ContentTypeForm = "application/x-www-form-urlencoded" )
Common content type.
Variables ¶
var DefaultSession = NewSession(http.DefaultClient)
DefaultSession is a wrapper of http.DefaultClient.
Functions ¶
func NewRequest ¶
func NewRequest(method, url string, opts ...RequestOption) (*http.Request, error)
NewRequest return a new *http.Request
func NewRequestWithContext ¶
func NewRequestWithContext(ctx context.Context, method, url string, opts ...RequestOption) (*http.Request, error)
NewRequestWithContext return a new *http.Request
Types ¶
type RequestOption ¶
type RequestOption func(o *RequestOptions)
RequestOption is used to update the fields in RequestOptions.
func Authorization ¶
func Authorization(a string) RequestOption
Authorization sets the request's Authorization header.
func BasicAuth ¶
func BasicAuth(username, password string) RequestOption
BasicAuth sets the request's Authorization header to use HTTP Basic Authentication with the provided username and password.
func ContentType ¶
func ContentType(ct string) RequestOption
ContentType sets the request's ContentType header.
func Cookies ¶
func Cookies(m map[string]string, replace ...bool) RequestOption
Cookies sets the request's cookies, if the replace flag is true, the existing cookies will be removed.
func File ¶
func File(fieldName string, file *os.File) RequestOption
File build the body for a multipart/form-data request.
func FileFromReader ¶
func FileFromReader(fieldName, fileName string, r io.Reader) RequestOption
FileFromReader build the body for a multipart/form-data request.
func Form ¶
func Form(m map[string]string) RequestOption
Form sets m as form format of request's body. ContentType will set to ContentTypeForm.
func Header ¶
func Header(k string, v interface{}) RequestOption
Header sets the request's header, v can be a string, fmt.Stringer or nil, when v is nil this header will be removed.
func Headers ¶
func Headers(m map[string]interface{}, replace ...bool) RequestOption
Headers sets the request's headers, value of map be a string, fmt.Stringer or nil, when v is nil this header will be removed. If the replace flag is true, the existing header will be removed.
func JSON ¶
func JSON(i interface{}) RequestOption
JSON marshal i to JSON format and sets it to request's body. ContentType will set to ContentTypeJSON.
func MultipartField ¶
func MultipartField(fieldName string, r io.Reader) RequestOption
MultipartField build the body for a multipart/form-data request.
func MultipartFieldBytes ¶
func MultipartFieldBytes(fieldName string, value []byte) RequestOption
MultipartFieldBytes build the body for a multipart/form-data request.
func MultipartFieldString ¶
func MultipartFieldString(fieldName, value string) RequestOption
MultipartFieldString build the body for a multipart/form-data request.
func Queries ¶
func Queries(m map[string]string, replace ...bool) RequestOption
Queries sets the request's queries.
func QueriesFromValue ¶
func QueriesFromValue(v url.Values) RequestOption
QueriesFromValue sets the request's queries.
func UserAgent ¶
func UserAgent(ua string) RequestOption
UserAgent sets the request's UserAgent header.
type RequestOptions ¶
type RequestOptions struct { Headers map[string]interface{} Queries url.Values Cookies map[string]string Body io.Reader Deadline time.Time Err error // contains filtered or unexported fields }
RequestOptions is a collection of request options.
type Response ¶
Response is wrapper of *http.Response.
func Connect ¶
func Connect(url string, opts ...RequestOption) (*Response, error)
Connect sends a CONNECT request.
func Delete ¶
func Delete(url string, opts ...RequestOption) (*Response, error)
Delete sends a DELETE request.
func Head ¶
func Head(url string, opts ...RequestOption) (*Response, error)
Head sends a HEAT request.
func Options ¶
func Options(url string, opts ...RequestOption) (*Response, error)
Options sends a OPTIONS request.
func Patch ¶
func Patch(url string, opts ...RequestOption) (*Response, error)
Patch sends a PATCH request.
func Post ¶
func Post(url string, opts ...RequestOption) (*Response, error)
Post sends a POST request.
func Request ¶
func Request(method, url string, opts ...RequestOption) (*Response, error)
Request sends an HTTP request and returns an HTTP response.
func Trace ¶
func Trace(url string, opts ...RequestOption) (*Response, error)
Trace sends a TRACE request.
func (*Response) StdResponse ¶
StdResponse return a unwrapped *http.Response.
type Session ¶
Session is a wrapper of *http.Client.
func (*Session) Connect ¶
func (s *Session) Connect(url string, opts ...RequestOption) (*Response, error)
Connect sends a CONNECT request.
func (*Session) Delete ¶
func (s *Session) Delete(url string, opts ...RequestOption) (*Response, error)
Delete sends a DELETE request.
func (*Session) Get ¶
func (s *Session) Get(url string, opts ...RequestOption) (*Response, error)
Get sends a GET request.
func (*Session) Head ¶
func (s *Session) Head(url string, opts ...RequestOption) (*Response, error)
Head sends a HEAT request.
func (*Session) Options ¶
func (s *Session) Options(url string, opts ...RequestOption) (*Response, error)
Options sends a OPTIONS request.
func (*Session) Patch ¶
func (s *Session) Patch(url string, opts ...RequestOption) (*Response, error)
Patch sends a PATCH request.
func (*Session) Post ¶
func (s *Session) Post(url string, opts ...RequestOption) (*Response, error)
Post sends a POST request.
func (*Session) Put ¶
func (s *Session) Put(url string, opts ...RequestOption) (*Response, error)
Put sends a PUT request.