goreq

package module
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: MIT Imports: 28 Imported by: 3

Documentation

Index

Constants

View Source
const (
	ContentType     = "Content-Type"
	ContentTypeJSON = "application/json; charset=UTF-8"
	ContentTypeXML  = "application/xml; charset=UTF-8"
	ContentTypeForm = "application/x-www-form-urlencoded; charset=UTF-8"

	Accept                 = "Accept"
	UserAgent              = "User-Agent"
	Referer                = "Referer"
	Origin                 = "Origin"
	ContentEncoding        = "Content-Encoding"
	ContentEncodingGzip    = "gzip"
	ContentEncodingDeflate = "deflate"
)

content type

View Source
const (
	StreamStateOpen    = "open"
	StreamStateClosed  = "closed"
	StreamEventMessage = "message"
)

Variables

View Source
var (
	ErrNoTransport      = errors.New("req: no transport")
	ErrNoURL            = errors.New("req: url not specified")
	ErrLackParam        = errors.New("req: lack param")
	ErrNoParser         = errors.New("resp: no parser")
	ErrNoFileMatch      = errors.New("req: no file match")
	ErrNotSupportedBody = errors.New("req: not supported body")
	ErrNoUnmarshal      = errors.New("resp: no unmarshal")
	ErrNoMarshal        = errors.New("req: no marshal")
	ErrParseStruct      = errors.New("req: can not parse struct param")
)

define errors

View Source
var (
	ErrStreamClosed               = errors.New("stream closed")
	ErrTooManyEmptyStreamMessages = errors.New("stream has sent too many empty messages")
)
View Source
var DefaultClient = NewClient()

Functions

This section is empty.

Types

type Client

type Client interface {
	Init(...Option) error
	Options() Options
	Clone(opts ...Option) Client
	Use(...HandlerFunc) Client
	Do(*Req) *Resp
	New() *Req
	Get(rawURL string) *Req
	Post(rawURL string) *Req
	Put(rawURL string) *Req
	Delete(rawURL string) *Req
	Head(rawURL string) *Req
}

func NewClient

func NewClient(opts ...Option) Client

func Use added in v0.2.6

func Use(handlers ...HandlerFunc) Client

Use handlers for DefaultClient

type Context added in v0.2.6

type Context struct {
	Req  *Req
	Resp *Resp
	// contains filtered or unexported fields
}

func (*Context) Abort added in v0.2.6

func (c *Context) Abort()

Abort prevents pending handlers from being called. Note that this will not stop the current handler.

func (*Context) AbortWithError added in v0.2.6

func (c *Context) AbortWithError(err error)

func (*Context) IsAborted added in v0.2.6

func (c *Context) IsAborted() bool

IsAborted returns true if the current context was aborted.

func (*Context) Next added in v0.2.6

func (c *Context) Next()

Next should be used only inside middleware. It executes the pending handlers in the chain inside the calling handler.

type FileUpload added in v0.2.6

type FileUpload struct {
	FieldName string        // form field name
	FileName  string        // filename in multipart form.
	File      io.ReadCloser // file to upload, required
}

FileUpload represents a file to upload

type HandlerChain added in v0.2.6

type HandlerChain []HandlerFunc

type HandlerFunc added in v0.2.6

type HandlerFunc func(*Context)

func CompressHandler added in v0.2.6

func CompressHandler() HandlerFunc

func DumpHandler added in v0.2.6

func DumpHandler() HandlerFunc

func Recovery added in v0.2.6

func Recovery() HandlerFunc

type Marshal added in v0.2.6

type Marshal func(interface{}) ([]byte, error)

type Option added in v0.2.6

type Option func(options *Options)

func EnableCookie added in v0.1.16

func EnableCookie(enable bool) Option

EnableCookie enable or disable cookie manager

func EnableInsecureTLS added in v0.1.16

func EnableInsecureTLS(enable bool) Option

EnableInsecureTLS allows insecure https

func WithCodec added in v0.2.6

func WithCodec(codec codec.Codec) Option

func WithPrefixPath added in v0.2.6

func WithPrefixPath(prefixPath string) Option

func WithProxy added in v0.1.16

func WithProxy(proxy func(*http.Request) (*url.URL, error)) Option

func WithProxyURL added in v0.1.16

func WithProxyURL(proxyURL string) Option

func WithTLSCert added in v0.1.16

func WithTLSCert(certPEMBlock, keyPEMBlock []byte) Option

func WithTimeout added in v0.1.16

func WithTimeout(timeout time.Duration) Option

func WithTransport added in v0.1.16

func WithTransport(transport http.RoundTripper) Option

type Options added in v0.2.6

type Options struct {
	EnableCookie          bool
	Timeout               time.Duration
	DialTimeout           time.Duration
	DialKeepAlive         time.Duration
	MaxIdleConns          int
	IdleConnTimeout       time.Duration
	TLSHandshakeTimeout   time.Duration
	ExpectContinueTimeout time.Duration
	Transport             http.RoundTripper
	TLSClientConfig       *tls.Config
	Proxy                 func(*http.Request) (*url.URL, error)
	Codecs                codec.Codecs
	PrefixPath            string // prefix path for all request
	Errors                []error
}

func NewOptions added in v0.2.6

func NewOptions() Options

type Req

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

Req represents a http request

func Delete added in v0.2.6

func Delete(rawURL string) *Req

Delete return a delete request

func Get

func Get(rawURL string) *Req

Get return a get request

func Head(rawURL string) *Req

Head return a head request

func New

func New() *Req

New return an empty request

func Post

func Post(rawURL string) *Req

Post return a post request

func Put added in v0.2.6

func Put(rawURL string) *Req

Put return a put request

func (*Req) AddCookie added in v0.2.6

func (r *Req) AddCookie(c *http.Cookie) *Req

AddCookie adds a cookie to the request.

func (*Req) AddFile added in v0.2.6

func (r *Req) AddFile(fieldName, fileName string, file io.ReadCloser) *Req

AddFile upload file with custom field name and file name

func (*Req) AddFileContent added in v0.2.6

func (r *Req) AddFileContent(fieldName, fileName string, content []byte) *Req

AddFileContent upload file with custom field name and file name

func (*Req) AddFiles added in v0.2.6

func (r *Req) AddFiles(patterns ...string) *Req

AddFiles upload file

func (*Req) AddFormParam added in v0.2.6

func (r *Req) AddFormParam(key string, value interface{}) *Req

AddFormParam add form parameter

func (*Req) AddFormParams added in v0.2.6

func (r *Req) AddFormParams(params map[string]interface{}) *Req

AddFormParams add multi form parameters

func (*Req) AddHeader added in v0.2.6

func (r *Req) AddHeader(key, value string) *Req

AddHeader add header

func (*Req) AddHeaders added in v0.2.6

func (r *Req) AddHeaders(headers map[string]string) *Req

AddHeaders add multi headers

func (*Req) AddQueryParam added in v0.2.6

func (r *Req) AddQueryParam(key string, value interface{}) *Req

AddQueryParam add query parameter

func (*Req) AddQueryParams added in v0.2.6

func (r *Req) AddQueryParams(params map[string]interface{}) *Req

AddQueryParams add multi query parameters

func (*Req) Build added in v0.2.6

func (r *Req) Build() (*http.Request, error)

Build request

func (*Req) Context added in v0.2.6

func (r *Req) Context() context.Context

Context return request context

func (*Req) Do

func (r *Req) Do() *Resp

Do is to call the request

func (*Req) Error

func (r *Req) Error() error

func (*Req) GetBody

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

GetBody return request body warn: file content can't be got when has upload files.

func (*Req) GetClient

func (r *Req) GetClient() Client

GetClient return client

func (*Req) GetFormParams added in v0.2.6

func (r *Req) GetFormParams() url.Values

GetFormParams return request form params

func (*Req) GetHeader added in v0.2.6

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

GetHeader return request headers

func (*Req) GetHost added in v0.2.6

func (r *Req) GetHost() string

GetHost return request host

func (*Req) GetLazyBody added in v0.2.6

func (r *Req) GetLazyBody() interface{}

GetLazyBody return lazy body

func (*Req) GetMethod added in v0.2.6

func (r *Req) GetMethod() string

GetMethod return request method

func (*Req) GetName added in v0.2.6

func (r *Req) GetName() string

GetName return name for this request

func (*Req) GetPath added in v0.2.6

func (r *Req) GetPath() string

GetPath return request path

func (*Req) GetQueryParams added in v0.2.6

func (r *Req) GetQueryParams() url.Values

GetQueryParams return request query params

func (*Req) GetURL added in v0.2.6

func (r *Req) GetURL() string

GetURL return request raw url

func (*Req) Use

func (r *Req) Use(handlers ...HandlerFunc) *Req

Use 仅在当前请求范围生效的中间件,不会改变到DefaultClient

func (*Req) WithAccept added in v0.2.6

func (r *Req) WithAccept(contentType string) *Req

WithAccept is to set Accept header

func (*Req) WithBasicAuth added in v0.2.6

func (r *Req) WithBasicAuth(username, password string) *Req

WithBasicAuth sets the request's Authorization header to use HTTP

func (*Req) WithBinaryBody added in v0.2.6

func (r *Req) WithBinaryBody(body []byte) *Req

WithBinaryBody add binary body

func (*Req) WithBody added in v0.2.6

func (r *Req) WithBody(body interface{}) *Req

WithBody add body

func (*Req) WithClient

func (r *Req) WithClient(c Client) *Req

WithClient with client

func (*Req) WithContentType added in v0.2.6

func (r *Req) WithContentType(contentType string) *Req

WithContentType is to set Content-Type header

func (*Req) WithContext added in v0.1.16

func (r *Req) WithContext(ctx context.Context) *Req

WithContext with context

func (*Req) WithFormParam added in v0.2.6

func (r *Req) WithFormParam(key string, value interface{}) *Req

WithFormParam with form parameter

func (*Req) WithFormParams added in v0.2.6

func (r *Req) WithFormParams(params map[string]interface{}) *Req

WithFormParams with multi form parameters

func (*Req) WithHeader added in v0.1.16

func (r *Req) WithHeader(key, value string) *Req

WithHeader set request method

func (*Req) WithJSONBody added in v0.1.17

func (r *Req) WithJSONBody(body interface{}) *Req

WithJSONBody convert body to json data

func (*Req) WithLazyBody added in v0.2.6

func (r *Req) WithLazyBody(lazyBody interface{}) *Req

WithLazyBody 仅将内容原封不动的保存在Req中,交由Handler对lazyBody处理后在转换为实际的Request中的body

func (*Req) WithMethod

func (r *Req) WithMethod(method string) *Req

WithMethod set request method

func (*Req) WithName added in v0.2.6

func (r *Req) WithName(name string) *Req

WithName to identify this request, for trace mostly.

func (*Req) WithOrigin added in v0.2.6

func (r *Req) WithOrigin(origin string) *Req

WithOrigin is to set Origin header

func (*Req) WithQueryParam added in v0.2.6

func (r *Req) WithQueryParam(key string, value interface{}) *Req

WithQueryParam with query parameter

func (*Req) WithQueryParams added in v0.2.6

func (r *Req) WithQueryParams(params map[string]interface{}) *Req

WithQueryParams with multi query parameters

func (*Req) WithReferer added in v0.2.6

func (r *Req) WithReferer(referer string) *Req

WithReferer is to set Referer header

func (*Req) WithStreamHeaders added in v0.3.0

func (r *Req) WithStreamHeaders() *Req

func (*Req) WithURL

func (r *Req) WithURL(rawURL string) *Req

WithURL set request raw url

func (*Req) WithUserAgent added in v0.2.6

func (r *Req) WithUserAgent(userAgent string) *Req

WithUserAgent is to set User-Agent header

func (*Req) WithXMLBody added in v0.1.17

func (r *Req) WithXMLBody(body interface{}) *Req

WithXMLBody convert body to xml data

type Resp

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

Resp represents a http response

func (*Resp) AsBytes

func (r *Resp) AsBytes() ([]byte, error)

AsBytes returns response body as []byte, return error if error happened when reading the response body

func (*Resp) AsFile

func (r *Resp) AsFile(dest string) error

AsFile save to file

func (*Resp) AsJSONMap

func (r *Resp) AsJSONMap() (map[string]interface{}, error)

func (*Resp) AsJSONStruct

func (r *Resp) AsJSONStruct(v interface{}) error

AsJSONStruct convert json response body to struct or map

func (*Resp) AsReader

func (r *Resp) AsReader() (io.Reader, error)

AsReader returns response body as reader

func (*Resp) AsStream added in v0.2.7

func (r *Resp) AsStream() *RespStream

AsStream for SSE(Server-Sent Events)

func (*Resp) AsString

func (r *Resp) AsString() (string, error)

AsString returns response body as string, return error if error happened when reading the response body

func (*Resp) AsStruct

func (r *Resp) AsStruct(v interface{}, unmarshal func([]byte, interface{}) error) error

AsStruct convert to struct. default to use json format

func (*Resp) AsXMLMap

func (r *Resp) AsXMLMap() (map[string]interface{}, error)

func (*Resp) AsXMLStruct

func (r *Resp) AsXMLStruct(v interface{}) error

AsXMLStruct convert xml response body to struct or map

func (*Resp) Bytes

func (r *Resp) Bytes() []byte

Bytes returns response body as []byte

func (*Resp) Consume

func (r *Resp) Consume()

Consume close response body

func (*Resp) ContentLength

func (r *Resp) ContentLength() int64

ContentLength returns content length

func (*Resp) ContentType

func (r *Resp) ContentType() string

ContentType returns content type

func (*Resp) Cost

func (r *Resp) Cost() time.Duration

Cost returns cost time

func (*Resp) Dump added in v0.2.6

func (r *Resp) Dump() string

func (*Resp) Error

func (r *Resp) Error() error

Error get error

func (*Resp) Request

func (r *Resp) Request() *http.Request

Request returns *http.Request

func (*Resp) Response

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

Response returns *http.Response

func (*Resp) SetError added in v0.2.6

func (r *Resp) SetError(err error)

func (*Resp) StatusCode

func (r *Resp) StatusCode() int

StatusCode returns status code

func (*Resp) String

func (r *Resp) String() string

String returns response body as string

func (*Resp) Timeout

func (r *Resp) Timeout() bool

Timeout returns true if timeout

type RespStream added in v0.3.0

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

func (*RespStream) Close added in v0.3.0

func (rs *RespStream) Close()

func (*RespStream) Read added in v0.3.0

func (rs *RespStream) Read() (eventName string, data string, err error)

func (*RespStream) State added in v0.3.0

func (rs *RespStream) State() string

type Unmarshal added in v0.2.6

type Unmarshal func([]byte, interface{}) error

Directories

Path Synopsis
xml
wechat Module
plugins
codec/sonic Module
prometheus Module
trace Module
vo module
wrapper
prometheus Module
trace Module

Jump to

Keyboard shortcuts

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