curl

package
v2.0.0-...-10e4721 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2024 License: MIT, MIT Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LreqHead  = 1 << iota // output request head (request line and request header)
	LreqBody              // output request body
	LrespHead             // output response head (response line and response header)
	LrespBody             // output response body
	Lcost                 // output time costed by the request
	LstdFlags = LreqHead | LreqBody | LrespHead | LrespBody
	Debug     = dbg(true)
)

flags to decide which part can be outputed

Variables

This section is empty.

Functions

func BodyJSON

func BodyJSON(v any) *bodyJson

BodyJSON make the object be encoded in json format and set it to the request body

func BodyRaw

func BodyRaw(v any) io.Reader

BodyRaw make the object for raw body. accepts string, []byte, io.Reader, io.ReadCloser

func BodyXML

func BodyXML(v any) *bodyXml

BodyXML make the object be encoded in xml format and set it to the request body

func Client

func Client() *http.Client

Client return the default underlying http client

func EnableCookie

func EnableCookie(enable bool)

EnableCookieenable or disable cookie manager

func EnableInsecureTLS

func EnableInsecureTLS(enable bool)

func File

func File(patterns ...string) any

File upload files matching the name pattern such as /usr/*/bin/go* (assuming the Separator is '/')

func Flags

func Flags() int

Flags return output format for the *Resp

func SetClient

func SetClient(client *http.Client)

SetClient sets the default curl.Client for requests.

func SetFlags

func SetFlags(flags int)

SetFlags control display format of *Resp

func SetJSONEscapeHTML

func SetJSONEscapeHTML(escape bool)

SetJSONEscapeHTML specifies whether problematic HTML characters should be escaped inside JSON quoted strings. The default behavior is to escape &, <, and > to \u0026, \u003c, and \u003e to avoid certain safety problems that can arise when embedding JSON in HTML.

In non-HTML settings where the escaping interferes with the readability of the output, SetEscapeHTML(false) disables this behavior.

func SetJSONIndent

func SetJSONIndent(prefix, indent string)

SetJSONIndent instructs the encoder to format each subsequent encoded value as if indented by the package-level function Indent(dst, src, prefix, indent). Calling SetIndent("", "") disables indentation.

func SetProgressInterval

func SetProgressInterval(interval time.Duration)

SetProgressInterval sets the progress reporting interval of both UploadProgress and DownloadProgress handler for the default client

func SetProxy

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

SetProxy sets the proxy for every request

func SetProxyUrl

func SetProxyUrl(rawurl string) error

SetProxyUrl set the simple proxy with fixed proxy url

func SetTimeout

func SetTimeout(d time.Duration)

SetTimeout sets the timeout for every request

func SetXMLIndent

func SetXMLIndent(prefix, indent string)

SetXMLIndent sets the encoder to generate XML in which each element begins on a new indented line that starts with prefix and is followed by one or more copies of indent according to the nesting depth.

Types

type BasicAuth

type BasicAuth struct {
	Username string
	Password string
}

type Cache

type Cache struct {
	Duration  time.Duration
	Interface memo.Interface
}

Cache set a cache time to keep request in memory

type DownloadProgress

type DownloadProgress func(current, total int64)

type FileUpload

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

FileUpload represents a file to upload

type Header map[string]string

Header represents http request header

func HeaderFromStruct

func HeaderFromStruct(v any) Header

HeaderFromStruct init header from struct

func ParseStruct

func ParseStruct(h Header, v any) Header

ParseStruct parse struct into header

func (Header) Clone

func (h Header) Clone() Header

type Host

type Host string

Host is used for set request's Host

type Param

type Param map[string]any

Param represents http request param

type QueryParam

type QueryParam map[string]any

QueryParam is used to force append http request param to the uri

type Req

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

Req is a convenient client for initiating requests

func New

func New() *Req

New create a new *Req

func (*Req) Client

func (r *Req) Client() *http.Client

Client return the default underlying http client

func (*Req) Delete

func (r *Req) Delete(url string, v ...any) (*Resp, error)

Delete execute a http DELETE request

func (*Req) Do

func (r *Req) Do(method, rawurl string, vs ...any) (resp *Resp, err error)

Do execute a http request with sepecify method and url, and it can also have some optional params, depending on your needs.

func (*Req) EnableCookie

func (r *Req) EnableCookie(enable bool)

EnableCookieenable or disable cookie manager

func (*Req) EnableInsecureTLS

func (r *Req) EnableInsecureTLS(enable bool)

EnableInsecureTLS allows insecure https

func (*Req) Flags

func (r *Req) Flags() int

Flags return output format for the *Resp

func (*Req) Get

func (r *Req) Get(url string, v ...any) (*Resp, error)

Get execute a http GET request

func (*Req) Head

func (r *Req) Head(url string, v ...any) (*Resp, error)

Head execute a http HEAD request

func (*Req) Options

func (r *Req) Options(url string, v ...any) (*Resp, error)

Options execute a http OPTIONS request

func (*Req) Patch

func (r *Req) Patch(url string, v ...any) (*Resp, error)

Patch execute a http PATCH request

func (*Req) Post

func (r *Req) Post(url string, v ...any) (*Resp, error)

Post execute a http POST request

func (*Req) Put

func (r *Req) Put(url string, v ...any) (*Resp, error)

Put execute a http PUT request

func (*Req) SetClient

func (r *Req) SetClient(client *http.Client)

SetClient sets the underlying curl.Client.

func (*Req) SetFlags

func (r *Req) SetFlags(flags int)

SetFlags control display format of *Resp

func (*Req) SetJSONEscapeHTML

func (r *Req) SetJSONEscapeHTML(escape bool)

SetJSONEscapeHTML specifies whether problematic HTML characters should be escaped inside JSON quoted strings. The default behavior is to escape &, <, and > to \u0026, \u003c, and \u003e to avoid certain safety problems that can arise when embedding JSON in HTML.

In non-HTML settings where the escaping interferes with the readability of the output, SetEscapeHTML(false) disables this behavior.

func (*Req) SetJSONIndent

func (r *Req) SetJSONIndent(prefix, indent string)

SetJSONIndent instructs the encoder to format each subsequent encoded value as if indented by the package-level function Indent(dst, src, prefix, indent). Calling SetIndent("", "") disables indentation.

func (*Req) SetProgressInterval

func (r *Req) SetProgressInterval(interval time.Duration)

SetProgressInterval sets the progress reporting interval of both UploadProgress and DownloadProgress handler

func (*Req) SetProxy

func (r *Req) SetProxy(proxy func(*http.Request) (*url.URL, error)) error

SetProxy sets the proxy for every request

func (*Req) SetProxyUrl

func (r *Req) SetProxyUrl(rawurl string) error

SetProxyUrl set the simple proxy with fixed proxy url

func (*Req) SetTimeout

func (r *Req) SetTimeout(d time.Duration)

SetTimeout sets the timeout for every request

func (*Req) SetXMLIndent

func (r *Req) SetXMLIndent(prefix, indent string)

SetXMLIndent sets the encoder to generate XML in which each element begins on a new indented line that starts with prefix and is followed by one or more copies of indent according to the nesting depth.

type ReservedHeader

type ReservedHeader map[string]string

type Resp

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

Resp represents a request with it's response

func Delete

func Delete(url string, v ...any) (*Resp, error)

Delete execute a http DELETE request

func Do

func Do(method, url string, v ...any) (*Resp, error)

Do execute request.

func Get

func Get(url string, v ...any) (*Resp, error)

Get execute a http GET request

func Head(url string, v ...any) (*Resp, error)

Head execute a http HEAD request

func Options

func Options(url string, v ...any) (*Resp, error)

Options execute a http OPTIONS request

func Patch

func Patch(url string, v ...any) (*Resp, error)

Patch execute a http PATCH request

func Post

func Post(url string, v ...any) (*Resp, error)

Post execute a http POST request

func Put

func Put(url string, v ...any) (*Resp, error)

Put execute a http PUT request

func (*Resp) Bytes

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

Bytes returns response body as []byte

func (*Resp) Cost

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

Cost return the time cost of the request

func (*Resp) Dump

func (r *Resp) Dump() string

Dump dump the request

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) String

func (r *Resp) String() string

String returns response body as string

func (*Resp) ToBytes

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

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

func (*Resp) ToFile

func (r *Resp) ToFile(name string) error

ToFile download the response body to file with optional download callback

func (*Resp) ToJSON

func (r *Resp) ToJSON(v any) error

ToJSON convert json response body to struct or map

func (*Resp) ToString

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

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

func (*Resp) ToXML

func (r *Resp) ToXML(v any) error

ToXML convert xml response body to struct or map

func (Resp) Upload

func (m Resp) Upload(req *http.Request)

type SerializedResponse

type SerializedResponse struct {
	ReqBody  []byte
	RespBody []byte
	Cost     time.Duration
}

type Timeout

type Timeout time.Duration

type UploadProgress

type UploadProgress func(current, total int64)

Jump to

Keyboard shortcuts

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