curl

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2023 License: MIT Imports: 18 Imported by: 1

Documentation

Index

Constants

View Source
const (
	ClientTimeout             = 15 * time.Second
	JsonContentType           = "application/json"
	JsonUTF8ContentType       = "application/json;charset=utf-8"
	FormUrlEncodedContentType = "application/x-www-form-urlencoded"
)

Variables

View Source
var Client = (func() *http.Client {
	t := http.DefaultTransport.(*http.Transport).Clone()
	t.MaxIdleConns = 100
	t.MaxConnsPerHost = 100
	t.MaxIdleConnsPerHost = 100
	return &http.Client{Transport: t, Timeout: ClientTimeout}
})()

Client Customize http.Client to optimize the performance based on the original http.DefaultTransport @see https://www.loginradius.com/blog/async/tune-the-go-http-client-for-high-performance

Functions

func NewGoCurl added in v1.1.5

func NewGoCurl() *goCurl

Types

type Request

type Request struct {
	Url      string
	Method   string
	Timeout  time.Duration
	Headers  map[string]string
	Cookies  map[string]string
	Queries  map[string]string
	PostData map[string]interface{}
	Body     io.Reader
	Response *Response
	// contains filtered or unexported fields
}

func NewRequest

func NewRequest(url string) *Request

NewRequest new request

func (*Request) DisableCloseConn added in v1.3.0

func (r *Request) DisableCloseConn() *Request

DisableCloseConn disable close connection

func (*Request) DisableTrace added in v1.3.0

func (r *Request) DisableTrace() *Request

DisableTrace disables trace.

func (*Request) DumpAll added in v1.3.0

func (r *Request) DumpAll() string

func (*Request) DumpRequest added in v1.3.0

func (r *Request) DumpRequest() string

func (*Request) DumpResponse added in v1.3.0

func (r *Request) DumpResponse() string

func (*Request) EnableCloseConn added in v1.3.0

func (r *Request) EnableCloseConn() *Request

EnableCloseConn closes the connection after sending this request and reading its response if set to true in HTTP/1.1 and HTTP/2. Setting this field prevents re-use of TCP connections between requests to the same hosts event if EnableKeepAlives() were called.

func (*Request) EnableTrace added in v1.3.0

func (r *Request) EnableTrace() *Request

EnableTrace enables trace (http3 currently does not support trace).

func (*Request) Get

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

Get init get request and return response

func (*Request) Post

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

Post init post request and return response

func (*Request) Send

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

Send init request and return response

func (*Request) SetBody

func (r *Request) SetBody(body interface{}) *Request

SetBody set the request Body, accepts string, []byte, io.Reader, io.ReadCloser.

func (*Request) SetContentType added in v1.3.0

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

SetContentType set the `Content-Type` for the request.

func (*Request) SetContext added in v1.3.0

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

SetContext method sets the context.Context for current Request. It allows to interrupt the request execution if ctx.Done() channel is closed. See https://blog.golang.org/context article and the "context" package documentation.

func (*Request) SetCookie added in v1.3.0

func (r *Request) SetCookie(key, value string) *Request

SetCookie set request cookie

func (*Request) SetCookies

func (r *Request) SetCookies(cookies map[string]string) *Request

SetCookies set request cookies

func (*Request) SetHeader added in v1.3.0

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

SetHeader set request header

func (*Request) SetHeaders

func (r *Request) SetHeaders(headers map[string]string) *Request

SetHeaders set request headers

func (*Request) SetMethod

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

SetMethod set request method

func (*Request) SetPostData

func (r *Request) SetPostData(data map[string]interface{}) *Request

SetPostData set post data

func (*Request) SetQueries

func (r *Request) SetQueries(queries map[string]string) *Request

SetQueries set request query

func (*Request) SetQuery added in v1.3.0

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

SetQuery set request query

func (*Request) SetTimeOut

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

SetTimeOut set request timeout after

func (*Request) SetUrl

func (r *Request) SetUrl(url string) *Request

SetUrl set request url

func (*Request) TraceInfo added in v1.3.0

func (r *Request) TraceInfo() TraceInfo

TraceInfo returns the trace information, only available if trace is enabled

type Response

type Response struct {
	Raw     *http.Response
	Request *Request
	Body    string
	// contains filtered or unexported fields
}

func NewResponse

func NewResponse() *Response

NewResponse new one response

func (*Response) GetContentType added in v1.3.0

func (r *Response) GetContentType() string

GetContentType return the `Content-Type` header value.

func (*Response) GetHeader added in v1.3.0

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

GetHeader returns the response header value by key.

func (*Response) GetHeaderValues added in v1.3.0

func (r *Response) GetHeaderValues(key string) []string

GetHeaderValues returns the response header values by key.

func (*Response) HeaderToString added in v1.3.0

func (r *Response) HeaderToString() string

HeaderToString get all header as string.

func (*Response) IsOk

func (r *Response) IsOk() bool

IsOk if response status code equal 200 return true

func (*Response) ReceivedAt added in v1.3.0

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

ReceivedAt returns the timestamp that response we received.

func (*Response) Status added in v1.3.0

func (r *Response) Status() string

Status returns the response status.

func (*Response) StatusCode

func (r *Response) StatusCode() int

StatusCode returns the response status code.

func (*Response) TotalTime added in v1.3.0

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

TotalTime returns the total time of the request, from request we sent to response we received.

func (*Response) TraceInfo added in v1.3.0

func (r *Response) TraceInfo() TraceInfo

TraceInfo returns the TraceInfo from Request.

type TraceInfo added in v1.3.0

type TraceInfo struct {
	// DNSLookupTime is a duration that transport took to perform
	// DNS lookup.
	DNSLookupTime time.Duration

	// ConnectTime is a duration that took to obtain a successful connection.
	ConnectTime time.Duration

	// TCPConnectTime is a duration that took to obtain the TCP connection.
	TCPConnectTime time.Duration

	// TLSHandshakeTime is a duration that TLS handshake took place.
	TLSHandshakeTime time.Duration

	// FirstResponseTime is a duration that server took to respond first byte since
	// connection ready (after tls handshake if it's tls and not a reused connection).
	FirstResponseTime time.Duration

	// ResponseTime is a duration since first response byte from server to
	// request completion.
	ResponseTime time.Duration

	// TotalTime is a duration that total request took end-to-end.
	TotalTime time.Duration

	// IsConnReused is whether this connection has been previously
	// used for another HTTP request.
	IsConnReused bool

	// IsConnWasIdle is whether this connection was obtained from an
	// idle pool.
	IsConnWasIdle bool

	// ConnIdleTime is a duration how long the connection was previously
	// idle, if IsConnWasIdle is true.
	ConnIdleTime time.Duration

	// LocalAddr returns the local network address.
	LocalAddr net.Addr

	// RemoteAddr returns the remote network address.
	RemoteAddr net.Addr
}

TraceInfo represents the trace information.

func (TraceInfo) String added in v1.3.0

func (t TraceInfo) String() string

String return the details of trace information.

Jump to

Keyboard shortcuts

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