httputil

package
v1.8.3 Latest Latest
Warning

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

Go to latest
Published: May 5, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package httputil implements http helpers

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AuthBearer

func AuthBearer(auth string) string

AuthBearer extracts bearer token value from Authorization header

func ExpectStatus added in v1.5.4

func ExpectStatus(want int, got int) error

ExpectStatus returns error if actual status not equal to expected one

func StdErrMsg

func StdErrMsg(code int) string

StdErrMsg returns standard error message by code

func ValidationErrMsg

func ValidationErrMsg(err validator.FieldError) string

ValidationErrMsg returns validation error message by validate tag

Types

type Client added in v1.7.0

type Client struct {
	BaseURL string
	Header  http.Header
	Cookies []http.Cookie
	Timeout time.Duration
}

Client implements base http client

func (*Client) DelJSON added in v1.7.0

func (c *Client) DelJSON(path string, payload any, destPtr any) (int, error)

DelJSON makes delete request and decodes response body as json

func (*Client) Get added in v1.7.0

func (c *Client) Get(path string) ([]byte, int, error)

Get makes get request and returns response body

func (*Client) GetJSON added in v1.7.0

func (c *Client) GetJSON(path string, destPtr any) (int, error)

GetJSON makes get request and decodes response body as json

func (*Client) PostJSON added in v1.7.0

func (c *Client) PostJSON(path string, payload any, destPtr any) (int, error)

PostJSON makes post request and decodes response body as json

func (*Client) PutJSON added in v1.7.0

func (c *Client) PutJSON(path string, payload any, destPtr any) (int, error)

PutJSON makes put request and decodes response body as json

func (*Client) Req added in v1.7.0

func (c *Client) Req(method string, path string, payload any) (*http.Response, error)

Req performs new http request with given method, path and optional json body

type Err

type Err struct {
	Code  int             `json:"code"`
	Msg   string          `json:"msg"`
	Items []ValidationErr `json:"items,omitempty"`
}

Err represents generic api error

type ErrResponse

type ErrResponse struct {
	Error Err `json:"error"`
}

ErrResponse represents json container for error object

func NewErr

func NewErr(code int, msg string) *ErrResponse

NewErr returns new standard api error value

func NewErrFrom

func NewErrFrom(err error) *ErrResponse

NewErrFrom returns new error value from given error

func NewValidationErr

func NewValidationErr(errors validator.ValidationErrors) *ErrResponse

NewValidationErr returns new validation error value

func (*ErrResponse) WriteJSON

func (e *ErrResponse) WriteJSON(w http.ResponseWriter)

WriteJSON responds json error with http.ResponseWriter

type ValidationErr

type ValidationErr struct {
	Subject string `json:"subject"`
	Msg     string `json:"msg"`
}

ValidationErr represents validation error

Jump to

Keyboard shortcuts

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