xhttp

package
v0.0.0-...-299d4c7 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2022 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ContentType header Content-Type
	ContentType = `Content-Type`
	// AcceptEncoding header Accept-Encoding
	AcceptEncoding = `Accept-Encoding`

	// ContentTypeURL application/x-www-form-urlencoded
	ContentTypeURL = `application/x-www-form-urlencoded; charset=utf-8`
	// ContentTypeJSON application/json
	ContentTypeJSON = `application/json; charset=utf-8`
	// ContentTypeXML application/xml
	ContentTypeXML = `application/xml; charset=utf-8`
	// ContentTypeMultipart multipart/form-data
	ContentTypeMultipart = `multipart/form-data`
)

const

Variables

This section is empty.

Functions

func ListenAndServe

func ListenAndServe(addr string, router http.Handler, option ServerOption) (es, e error)

ListenAndServe new server and start

func ListenAndServeV2

func ListenAndServeV2(addr string, router http.Handler, option ServerOption) (es, e error)

ListenAndServeV2 new http2 server and start

func Redirect

func Redirect(w http.ResponseWriter, r *http.Request, url string)

Redirect redirect

func SetHeader

func SetHeader(w http.ResponseWriter, key, value string)

SetHeader set http response header

func WriteBytes

func WriteBytes(w http.ResponseWriter, response interface{}) error

WriteBytes response bytes

func WriteJSON

func WriteJSON(w http.ResponseWriter, response interface{}) error

WriteJSON response JSON data.

func WriteXML

func WriteXML(w http.ResponseWriter, response interface{}) error

WriteXML response XML data.

Types

type Message

type Message struct {
	StatusCode int
	Body       []byte
	Header     http.Header
	// contains filtered or unexported fields
}

Message HTTP response

func Connect

func Connect(uri string, body io.Reader, opt RequestOption) (msg Message, err error)

Connect HTTP request CONNECT

func Delete

func Delete(uri string, body io.Reader, opt RequestOption) (msg Message, err error)

Delete HTTP request DELETE

func Get

func Get(uri string, opt RequestOption) (msg Message, err error)

Get HTTP request GET

func GetJSON

func GetJSON(v interface{}, uri string, opt RequestOption) (msg Message, err error)

GetJSON HTTP request GET, response JSON

func GetXML

func GetXML(v interface{}, uri string, opt RequestOption) (msg Message, err error)

GetXML HTTP request GET, response XML

func Head(uri string, opt RequestOption) (msg Message, err error)

Head HTTP request HEAD

func Options

func Options(uri string, body io.Reader, opt RequestOption) (msg Message, err error)

Options HTTP request OPTIONS

func Patch

func Patch(uri string, body io.Reader, opt RequestOption) (msg Message, err error)

Patch HTTP request PATCH

func Post

func Post(uri string, body io.Reader, opt RequestOption) (msg Message, err error)

Post HTTP request POST

func Put

func Put(uri string, body io.Reader, opt RequestOption) (msg Message, err error)

Put HTTP request PUT

func Trace

func Trace(uri string, body io.Reader, opt RequestOption) (msg Message, err error)

Trace HTTP request TRACE

func (Message) Cookies

func (m Message) Cookies() []*http.Cookie

Cookies parses and returns the cookies set in the Set-Cookie headers.

func (Message) JSON

func (m Message) JSON(v interface{}) error

JSON Body to JSON

func (Message) Location

func (m Message) Location() (*url.URL, error)

Location returns the URL of the response's "Location" header, if present. Relative redirects are resolved relative to the Response's Request. ErrNoLocation is returned if no Location header is present.

func (Message) Referer

func (m Message) Referer() string

Referer returns the referring URL, if sent in the request.

func (Message) UserAgent

func (m Message) UserAgent() string

UserAgent returns the client's User-Agent, if sent in the request.

func (Message) XML

func (m Message) XML(v interface{}) error

XML Body to XML

type RequestOption

type RequestOption struct {
	RequestTimeOut        time.Duration
	ResponseHeaderTimeout time.Duration

	Headers map[string]string
	Cookies []*http.Cookie

	CertFile string
	KeyFile  string
	// contains filtered or unexported fields
}

RequestOption HTTP request option

func DefaultRequestOption

func DefaultRequestOption() RequestOption

DefaultRequestOption default request option

func (*RequestOption) AcceptEncodingGZIP

func (r *RequestOption) AcceptEncodingGZIP() *RequestOption

AcceptEncodingGZIP Accept-Encoding gzip

func (*RequestOption) AddCookie

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

AddCookie adds a cookie to the request.

func (*RequestOption) SetBasicAuth

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

SetBasicAuth sets the request's Authorization header to use HTTP Basic Authentication with the provided username and password.

func (*RequestOption) SetCert

func (r *RequestOption) SetCert(certFile, keyFile string) *RequestOption

SetCert set request cert & key file

func (*RequestOption) SetContentType

func (r *RequestOption) SetContentType(v string) *RequestOption

SetContentType set Content-Type

func (*RequestOption) SetContentTypeJSON

func (r *RequestOption) SetContentTypeJSON() *RequestOption

SetContentTypeJSON Content-Type json

func (*RequestOption) SetContentTypeURL

func (r *RequestOption) SetContentTypeURL() *RequestOption

SetContentTypeJSON Content-Type json

func (*RequestOption) SetContentTypeXML

func (r *RequestOption) SetContentTypeXML() *RequestOption

SetContentTypeJSON Content-Type json

func (*RequestOption) SetHeader

func (r *RequestOption) SetHeader(k, v string) *RequestOption

SetHeader set header

func (*RequestOption) SetResponseHeaderTimeout

func (r *RequestOption) SetResponseHeaderTimeout(v time.Duration) *RequestOption

SetResponseHeaderTimeout set response header timeout

func (*RequestOption) SetTimeOut

func (r *RequestOption) SetTimeOut(v time.Duration) *RequestOption

SetTimeOut set request timeout

type ServerOption

type ServerOption struct {
	CertFile string
	KeyFile  string

	// ReadHeaderTimeout is the amount of time allowed to read request headers
	ReadHeaderTimeout time.Duration
	// ReadTimeout is the maximum duration for reading the entire
	// request, including the body.
	ReadTimeout time.Duration
	// WriteTimeout is the maximum duration before timing out
	// writes of the response.
	WriteTimeout time.Duration
	// IdleTimeout is the maximum amount of time to wait for the
	// next request when keep-alives are enabled.
	IdleTimeout time.Duration

	// MaxHeaderBytes controls the maximum number of bytes the
	// server will read parsing the request header's keys and
	// values, including the request line. It does not limit the
	// size of the request body.
	// If zero, DefaultMaxHeaderBytes is used.
	MaxHeaderBytes int

	ShutdownTimeout time.Duration
}

ServerOption serve option

func DefaultServerOption

func DefaultServerOption() ServerOption

func (*ServerOption) SetCertFile

func (s *ServerOption) SetCertFile(path string) *ServerOption

SetCertFile set CertFile

func (*ServerOption) SetIdleTimeout

func (s *ServerOption) SetIdleTimeout(t time.Duration) *ServerOption

SetIdleTimeout set IdleTimeout

func (*ServerOption) SetKeyFile

func (s *ServerOption) SetKeyFile(path string) *ServerOption

SetKeyFile set KeyFile

func (*ServerOption) SetMaxHeaderBytes

func (s *ServerOption) SetMaxHeaderBytes(n int) *ServerOption

SetMaxHeaderBytes set MaxHeaderBytes

func (*ServerOption) SetReadHeaderTimeout

func (s *ServerOption) SetReadHeaderTimeout(t time.Duration) *ServerOption

SetReadHeaderTimeout set ReadHeaderTimeout

func (*ServerOption) SetReadTimeout

func (s *ServerOption) SetReadTimeout(t time.Duration) *ServerOption

SetReadTimeout set ReadTimeout

func (*ServerOption) SetShutdownTimeout

func (s *ServerOption) SetShutdownTimeout(t time.Duration) *ServerOption

SetShutdownTimeout set ShutdownTimeout

func (*ServerOption) SetWriteTimeout

func (s *ServerOption) SetWriteTimeout(t time.Duration) *ServerOption

SetWriteTimeout set WriteTimeout

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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