request

package
v0.0.0-...-e05b1bb Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	FormContent  = "application/x-www-form-urlencoded"
	JSONContent  = "application/json"
	PlainContent = "text/plain"
	XMLContent   = "application/xml"

	// URLEncoding specifies application/x-www-form-urlencoded
	URLEncoding = map[string]string{"Content-Type": FormContent}

	// JSONEncoding specifies application/json
	JSONEncoding = map[string]string{
		"Content-Type": JSONContent,
		"Accept":       JSONContent,
	}

	// AcceptJSON accepting application/json
	AcceptJSON = map[string]string{
		"Accept": JSONContent,
	}

	// XMLEncoding specifies application/xml
	XMLEncoding = map[string]string{
		"Content-Type": XMLContent,
		"Accept":       XMLContent,
	}

	// AcceptXML accepting application/xml
	AcceptXML = map[string]string{
		"Accept": XMLContent,
	}
)
View Source
var (
	LogHeaders bool
	LogMaxLen  = 1024 * 8
)
View Source
var Timeout = 10 * time.Second

Timeout is the default request timeout used by the Helper

Functions

func DontFollow

func DontFollow(req *http.Request, via []*http.Request) error

DontFollow is a redirect policy that does not follow redirects

func MarshalJSON

func MarshalJSON(data interface{}) io.ReadSeeker

MarshalJSON marshals JSON into an io.ReadSeeker

func New

func New(method, uri string, data io.Reader, headers ...map[string]string) (*http.Request, error)

New builds and executes HTTP request and returns the response

func NewClient

func NewClient(log *util.Logger) *http.Client

NewClient creates http client with default transport

func NewTripper

func NewTripper(log *util.Logger, base http.RoundTripper) http.RoundTripper

NewTripper creates a logging roundtrip handler

func ReadBody

func ReadBody(resp *http.Response) ([]byte, error)

ReadBody reads HTTP response and returns error on response codes other than HTTP 2xx. It closes the request body after reading.

func ResponseError

func ResponseError(resp *http.Response) error

ResponseError turns an HTTP status code into an error

Types

type Helper

type Helper struct {
	*http.Client
}

Helper provides utility primitives

func NewHelper

func NewHelper(log *util.Logger) *Helper

NewHelper creates http helper for simplified PUT GET logic

func (*Helper) DoBody

func (r *Helper) DoBody(req *http.Request) ([]byte, error)

DoBody executes HTTP request and returns the response body

func (*Helper) DoJSON

func (r *Helper) DoJSON(req *http.Request, res interface{}) error

DoJSON executes HTTP request and decodes JSON response. It returns a StatusError on response codes other than HTTP 2xx.

func (*Helper) GetBody

func (r *Helper) GetBody(url string) ([]byte, error)

GetBody executes HTTP GET request and returns the response body

func (*Helper) GetJSON

func (r *Helper) GetJSON(url string, res interface{}) error

GetJSON executes HTTP GET request and decodes JSON response. It returns a StatusError on response codes other than HTTP 2xx.

type InterceptResult

type InterceptResult = func() (string, error)

func InterceptRedirect

func InterceptRedirect(param string, stop bool) (func(req *http.Request, via []*http.Request) error, InterceptResult)

InterceptRedirect captures a redirect url parameter

type StatusError

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

StatusError indicates unsuccessful http response

func NewStatusError

func NewStatusError(resp *http.Response) StatusError

NewStatusError create new StatusError for given response

func (StatusError) Error

func (e StatusError) Error() string

func (StatusError) HasStatus

func (e StatusError) HasStatus(codes ...int) bool

HasStatus returns true if the response's status code matches any of the given codes

func (StatusError) Response

func (e StatusError) Response() *http.Response

Response returns the response with the unexpected error

func (StatusError) StatusCode

func (e StatusError) StatusCode() int

StatusCode returns the response's status code

Jump to

Keyboard shortcuts

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