request

package
v0.0.0-...-e6e1b3f Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2021 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

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

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

	// AcceptJSON accepting application/json
	AcceptJSON = map[string]string{
		"Accept": "application/json",
	}
)
View Source
var Timeout = 10 * time.Second

Timeout is the default request timeout used by the Helper

Functions

func DecodeJSON

func DecodeJSON(resp *http.Response, res interface{}) error

DecodeJSON reads HTTP response and decodes JSON body if error is nil

func DefaultTransport

func DefaultTransport() *http.Transport

DefaultTransport returns http.DefaultTransport as http.Transport instead of http.RoundTripper

func InsecureTransport

func InsecureTransport() *http.Transport

InsecureTransport is an http.Transport with TLSClientConfig.InsecureSkipVerify enabled

func MarshalJSON

func MarshalJSON(data interface{}) io.Reader

MarshalJSON marshals JSON into an io.Reader

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 NewTripper

func NewTripper(log *util.Logger, transport 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.

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

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

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 respose's status code matches any of the given codes

func (StatusError) Response

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

Response returns the respose with the unexpected error

func (StatusError) StatusCode

func (e StatusError) StatusCode() int

StatusCode returns the respose's status code

Jump to

Keyboard shortcuts

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