http

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2023 License: MIT Imports: 11 Imported by: 10

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseJsonResponse

func ParseJsonResponse[T any](resp Response) (data T, err error)

Types

type Client

type Client interface {
	ExecuteRequest(req Request) Response
}

func NewClient

func NewClient() Client

type ContentType

type ContentType = string
const (
	ContentTypeJson       ContentType = "application/json"
	ContentTypeForm       ContentType = "application/x-www-form-urlencoded"
	ContentTypeFileStream ContentType = "application/octet-stream"
	ContentTypeMultipart  ContentType = "multipart/form-data"
	ContentTypeTextPlain  ContentType = "text/plain"
	ContentTypeTextHtml   ContentType = "text/html"
	ContentTypeTextXml    ContentType = "text/xml"
	ContentTypeTextYaml   ContentType = "text/yaml"
	ContentTypeTextCsv    ContentType = "text/csv"
	ContentTypeImagePng   ContentType = "image/png"
	ContentTypeImageJpeg  ContentType = "image/jpeg"
	ContentTypeImageGif   ContentType = "image/gif"
)

type Method

type Method = string
const (
	GET     Method = "GET"
	POST    Method = "POST"
	OPTIONS Method = "OPTIONS"
	HEAD    Method = "HEAD"
	PUT     Method = "PUT"
	DELETE  Method = "DELETE"
	TRACE   Method = "TRACE"
	CONNECT Method = "CONNECT"
	PATCH   Method = "PATCH"
)

type Request

type Request interface {
	SetUrl(path string) Request
	SetMethod(method string) Request
	SetBearerToken(token string) Request
	SetUserAgent(userAgent string) Request
	SetAccept(accept string) Request
	SetHeader(key, value string) Request
	SetJsonBody(ptr any) Request
	SetMultiPartBody(multipartField, multipartName string, multiWriter io.Reader, others map[string]string) Request
	SetCookie(cookieKey, cookieValue string) Request
	// contains filtered or unexported methods
}

func NewGetRequest

func NewGetRequest(url string) Request

func NewPostRequest

func NewPostRequest(url string, payload any) Request

func NewRequest

func NewRequest() Request

type Response

type Response interface {
	Error() error
	Result() (status int, body []byte, err error)
	StringResult() (status int, body string, err error)
	BindJsonResult(receiver any) (status int, err error)
	GetHeader(key string, defaultVal ...string) (val string)
	GetBearerToken() (token string)
	GetCookie(cookieName string) (ck *http.Cookie)
	GetStatusCode() int
	GetBody() (body []byte)
	BindJsonBody(receiver any) (err error)
}

func NewResponse

func NewResponse(r *http.Response, e error) Response

type UserAgent

type UserAgent = string
const (
	Curl      UserAgent = "curl/7.64.1"
	Postman   UserAgent = "PostmanRuntime/7.26.8"
	ChromeOSX UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
	Safari    UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Safari/605.1.15"
	Firefox   UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/119.0"
)

Jump to

Keyboard shortcuts

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