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
- func DecodeAndValidateJSONPayload(request *http.Request, v PayloadValidation) error
- func GetPayload[T any, PT interface{ ... }](r *http.Request) (PT, error)
- func RetrieveBooleanMultiPartFormValue(request *http.Request, name string, optional bool) (bool, error)
- func RetrieveBooleanQueryParameter(request *http.Request, name string, optional bool) (bool, error)
- func RetrieveJSONQueryParameter(request *http.Request, name string, target interface{}, optional bool) error
- func RetrieveMultiPartFormFile(request *http.Request, requestParameter string) ([]byte, string, error)
- func RetrieveMultiPartFormJSONValue(request *http.Request, name string, target interface{}, optional bool) error
- func RetrieveMultiPartFormValue(request *http.Request, name string, optional bool) (string, error)
- func RetrieveNumericMultiPartFormValue(request *http.Request, name string, optional bool) (int, error)
- func RetrieveNumericQueryParameter(request *http.Request, name string, optional bool) (int, error)
- func RetrieveNumericRouteVariableValue(request *http.Request, name string) (int, error)
- func RetrieveQueryParameter(request *http.Request, name string, optional bool) (string, error)
- func RetrieveRouteVariableValue(request *http.Request, name string) (string, error)
- type PayloadValidation
Constants ¶
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 ¶
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 ¶
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 ¶
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 ¶
RetrieveNumericRouteVariableValue returns the value of a route variable as an integer.
func RetrieveQueryParameter ¶
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.
Types ¶
type PayloadValidation ¶
PayloadValidation is an interface used to validate the payload of a request.