request

package
v0.0.0-...-c3cd77f Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2023 License: Zlib Imports: 7 Imported by: 70

Documentation

Overview

Package request provides function to retrieve content from a *http.Request object of the net/http standard library, be it JSON body payload, multi-part form values or query parameters. It also provides functions to retrieve route variables when using gorilla/mux.

Index

Constants

View Source
const (
	// ErrInvalidQueryParameter defines the message of an error raised when a mandatory query parameter has an invalid value.
	ErrInvalidQueryParameter = "Invalid query parameter"
	// ErrInvalidRequestURL defines the message of an error raised when the data sent in the query or the URL is invalid
	ErrInvalidRequestURL = "Invalid request URL"
	// ErrMissingQueryParameter defines the message of an error raised when a mandatory query parameter is missing.
	ErrMissingQueryParameter = "Missing query parameter"
	// ErrMissingFormDataValue defines the message of an error raised when a mandatory form data value is missing.
	ErrMissingFormDataValue = "Missing form data value"
)

Variables

This section is empty.

Functions

func DecodeAndValidateJSONPayload

func DecodeAndValidateJSONPayload(request *http.Request, v PayloadValidation) error

DecodeAndValidateJSONPayload decodes the body of the request into an object implementing the PayloadValidation interface. It also triggers a validation of object content.

func GetPayload

func GetPayload[T any, PT interface {
	*T
	Validate(request *http.Request) error
}](r *http.Request) (PT, error)

GetPayload decodes the body of the request into an object implementing the PayloadValidation interface.

func RetrieveBooleanMultiPartFormValue

func RetrieveBooleanMultiPartFormValue(request *http.Request, name string, optional bool) (bool, error)

RetrieveBooleanMultiPartFormValue returns the value of some form data as a boolean. If optional is set to true, will not return an error when the form data value is not found.

func RetrieveBooleanQueryParameter

func RetrieveBooleanQueryParameter(request *http.Request, name string, optional bool) (bool, error)

RetrieveBooleanQueryParameter returns the value of a query parameter as a boolean. If optional is set to true, will not return an error when the query parameter is not found.

func RetrieveJSONQueryParameter

func RetrieveJSONQueryParameter(request *http.Request, name string, target interface{}, optional bool) error

RetrieveJSONQueryParameter decodes the value of a query parameter as a JSON object into the target parameter. If optional is set to true, will not return an error when the query parameter is not found.

func RetrieveMultiPartFormFile

func RetrieveMultiPartFormFile(request *http.Request, requestParameter string) ([]byte, string, error)

RetrieveMultiPartFormFile returns the content of an uploaded file (form data) as bytes as well as the name of the uploaded file.

func RetrieveMultiPartFormJSONValue

func RetrieveMultiPartFormJSONValue(request *http.Request, name string, target interface{}, optional bool) error

RetrieveMultiPartFormJSONValue decodes the value of some form data as a JSON object into the target parameter. If optional is set to true, will not return an error when the form data value is not found.

func RetrieveMultiPartFormValue

func RetrieveMultiPartFormValue(request *http.Request, name string, optional bool) (string, error)

RetrieveMultiPartFormValue returns the value of some form data as a string. If optional is set to true, will not return an error when the form data value is not found.

func RetrieveNumericMultiPartFormValue

func RetrieveNumericMultiPartFormValue(request *http.Request, name string, optional bool) (int, error)

RetrieveNumericMultiPartFormValue returns the value of some form data as an integer. If optional is set to true, will not return an error when the form data value is not found.

func RetrieveNumericQueryParameter

func RetrieveNumericQueryParameter(request *http.Request, name string, optional bool) (int, error)

RetrieveNumericQueryParameter returns the value of a query parameter as an integer. If optional is set to true, will not return an error when the query parameter is not found.

func RetrieveNumericRouteVariableValue

func RetrieveNumericRouteVariableValue(request *http.Request, name string) (int, error)

RetrieveNumericRouteVariableValue returns the value of a route variable as an integer.

func RetrieveQueryParameter

func RetrieveQueryParameter(request *http.Request, name string, optional bool) (string, error)

RetrieveQueryParameter returns the value of a query parameter as a string. If optional is set to true, will not return an error when the query parameter is not found.

func RetrieveRouteVariableValue

func RetrieveRouteVariableValue(request *http.Request, name string) (string, error)

RetrieveRouteVariableValue returns the value of a route variable as a string.

Types

type PayloadValidation

type PayloadValidation interface {
	Validate(request *http.Request) error
}

PayloadValidation is an interface used to validate the payload of a request.

Jump to

Keyboard shortcuts

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