httpx

package module
v0.0.0-...-d6f4114 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2022 License: MIT Imports: 17 Imported by: 0

README

httpx

HTTP utilities for the Go standard library

Documentation

Index

Constants

View Source
const (
	XForwardedFor = "X-Forwarded-Proto"
)

Variables

View Source
var (
	ErrInternalServer = Error{
		Message: "internal server error",
		Code:    "INTERNAL_SERVER_ERROR",
		Status:  http.StatusInternalServerError,
	}

	ErrNotFound = Error{
		Message: "resource not found",
		Code:    "NOT_FOUND_ERROR",
		Status:  http.StatusNotFound,
	}

	ErrUnauthorized = Error{
		Message: "unauthorized",
		Code:    "UNAUTHORIZED",
		Status:  http.StatusUnauthorized,
	}

	ErrBadRequest = Error{
		Message: "bad request",
		Code:    "BAD_REQUEST",
		Status:  http.StatusBadRequest,
	}
)
View Source
var (
	ErrNoHTTPBody = errors.New("no http body bytes to read")
)

Functions

func BodyAs

func BodyAs[T any](r *http.Request) (T, error)

func BodyReadAll

func BodyReadAll(r *http.Request) ([]byte, error)

func DecryptCookie

func DecryptCookie[T any](opts SecureCookieParams) (*T, error)

func DefaultCookieEncSecret

func DefaultCookieEncSecret() []byte

func DefaultCookieSigningSecret

func DefaultCookieSigningSecret() []byte

func EncryptCookie

func EncryptCookie(opts SecureCookieParams) (*http.Cookie, error)

func HasBody

func HasBody(r *http.Request) bool

func IsSecure

func IsSecure(r *http.Request) bool

func NativeHandlerFunc

func NativeHandlerFunc(h HandlerFunc) http.HandlerFunc

func ProxyBaseURL

func ProxyBaseURL(r *http.Request) url.URL

func ProxyURLFull

func ProxyURLFull(r *http.Request) url.URL

Types

type ContentType

type ContentType string
const (
	TextHTML        ContentType = "text/html; charset=utf-8"
	ApplicationJSON ContentType = "application/json; charset=utf-8"
	ApplicationXML  ContentType = "application/xml; charset=utf-8"
)

type ErrCtx

type ErrCtx ResMap

type Error

type Error struct {
	Message string `json:"message"`
	Code    string `json:"code"`
	Context ResMap `json:"context"`

	Err    error `json:"-"`
	Status int   `json:"-"`
}

func (Error) Error

func (e Error) Error() string

func (Error) Unwrap

func (e Error) Unwrap() error

Unwrap provides compatibility for Go 1.13 error chains.

func (Error) WithErr

func (e Error) WithErr(err error) Error

func (Error) WithErrCtx

func (e Error) WithErrCtx(ctx ErrCtx) Error

func (Error) WithMessage

func (e Error) WithMessage(msg string) Error

type HandlerFunc

type HandlerFunc func(w ResponseWriter, r *http.Request) error

func (HandlerFunc) ServeHTTP

func (h HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request)

type ResMap

type ResMap map[string]interface{}

type ResponseWriter

type ResponseWriter interface {
	Status(code int) ResponseWriter
	ContentType(media ContentType) ResponseWriter
	Send(a ...any) error

	JSON(v any) error
	XML(v any) error

	JSONErr(err Error) error
	XMLErr(err Error) error

	Header() http.Header
	Write(bytes []byte) (int, error)
	WriteHeader(statusCode int)

	Redirect(r *http.Request, url string, code int)
	SetCookie(cookie http.Cookie) ResponseWriter
	DelCookie(cookie http.Cookie) ResponseWriter
	DelCookieByName(name string) ResponseWriter
}

func NewResponseWriter

func NewResponseWriter(w http.ResponseWriter) ResponseWriter

type SecureCookieParams

type SecureCookieParams struct {
	Cookie        *http.Cookie
	SigningSecret []byte
	EncryptSecret []byte
	Data          any
}

Jump to

Keyboard shortcuts

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