Documentation
¶
Index ¶
- Variables
- func Bytes(r *http.Request) ([]byte, error)
- func Cookie(r *http.Request, k string) *http.Cookie
- func CookieValue(r *http.Request, k string) string
- func CookieValueOr(r *http.Request, k string, d string) string
- func HasHeader(r *http.Request, key string) bool
- func HasQuery(r *http.Request, k string) bool
- func Header(r *http.Request, key string) string
- func HeaderOr(r *http.Request, key string, def string) string
- func HeaderValues(r *http.Request, key string) []string
- func Hooks(r *http.Request) contract.Hooks
- func JSON[T any](r *http.Request) (value T, err error)
- func MustSession(r *http.Request) contract.Session
- func MustSessionKeyed(r *http.Request, key any) contract.Session
- func Param(r *http.Request, k string) string
- func ParamOr(r *http.Request, k string, d string) string
- func Query(r *http.Request, k string) string
- func QueryOr(r *http.Request, k string, d string) string
- func Session(r *http.Request) (contract.Session, bool)
- func SessionKeyed(r *http.Request, key any) (contract.Session, bool)
- func String(r *http.Request) (string, error)
- func XML[T any](r *http.Request) (value T, err error)
Constants ¶
This section is empty.
Variables ¶
var ErrNoHooksMiddleware = problem.Problem{ Title: "No hooks context", Detail: "Unable to resolve hooks as there's no context value", Status: http.StatusInternalServerError, }
var ErrSessionNotFound = problem.Problem{ Title: "Session not found", Detail: "Unable to find the session in the request", Status: http.StatusInternalServerError, }
Functions ¶
func Bytes ¶
Bytes reads the entire request body and returns it as a byte slice. This function consumes the request body, so it can only be called once per request. The request body is automatically closed after reading.
Parameters:
- r: The HTTP request containing the body to read
Returns the raw byte data from the request body, or an error if reading fails.
func Cookie ¶
Cookie retrieves a cookie by name from the HTTP request. Returns the cookie if found, or nil if the cookie doesn't exist or if there's an error retrieving it.
Parameters:
- r: The HTTP request to search for the cookie
- k: The name of the cookie to retrieve
Returns the cookie object or nil if not found.
func CookieValue ¶
CookieValue retrieves the value of a cookie by name from the HTTP request. This is a convenience function that extracts just the value from the cookie. Returns an empty string if the cookie doesn't exist.
Parameters:
- r: The HTTP request to search for the cookie
- k: The name of the cookie whose value to retrieve
Returns the cookie value as a string, or empty string if not found.
func CookieValueOr ¶
CookieValueOr retrieves the value of a cookie by name, returning a default value if the cookie doesn't exist or has an empty value. This is useful for providing fallback values when cookies are optional.
Parameters:
- r: The HTTP request to search for the cookie
- k: The name of the cookie whose value to retrieve
- d: The default value to return if the cookie is not found or empty
Returns the cookie value if found and non-empty, otherwise the default value.
func HasQuery ¶
HasQuery checks if a query parameter exists in the HTTP request URL, regardless of its value. This is useful for distinguishing between a parameter that doesn't exist and one that exists but has an empty value.
Parameters:
- r: The HTTP request containing the URL with query parameters
- k: The name of the query parameter to check for
Returns true if the parameter exists in the query string, false otherwise.
func Hooks ¶
Hooks return the hooks struct to attach callbacks to certain lifecycle events. It panics if no context value is found. Make sure you use the hooks middleware before using this method.
func JSON ¶
JSON decodes JSON data from the request body into a value of type T. It uses streaming JSON decoding for memory efficiency and automatically closes the request body after reading. The function uses generics to provide type safety for the decoded result.
The target type T should be a pointer to the desired struct or type that matches the expected JSON structure.
Parameters:
- r: The HTTP request containing JSON data in the body
Returns the decoded value of type T, or an error if decoding fails.
func Param ¶
Param retrieves a path parameter value by name from the HTTP request. This uses Go's built-in PathValue method which extracts values from URL path patterns like "/users/{id}" where {id} is the parameter name.
Parameters:
- r: The HTTP request containing the path parameters
- k: The name of the path parameter to retrieve
Returns the parameter value as a string, or empty string if not found.
func ParamOr ¶
ParamOr retrieves a path parameter value by name, returning a default value if the parameter doesn't exist or is empty. This is useful for providing fallback values when path parameters are optional or when you want to handle missing parameters gracefully.
Parameters:
- r: The HTTP request containing the path parameters
- k: The name of the path parameter to retrieve
- d: The default value to return if the parameter is not found or empty
Returns the parameter value if found and non-empty, otherwise the default value.
func Query ¶
Query retrieves a query parameter value by name from the HTTP request URL. This extracts values from the URL query string like "?name=value&other=test" where the parameter name matches the provided key.
Parameters:
- r: The HTTP request containing the URL with query parameters
- k: The name of the query parameter to retrieve
Returns the first value associated with the key, or empty string if not found.
func QueryOr ¶
QueryOr retrieves a query parameter value by name, returning a default value if the parameter doesn't exist. Note that if the parameter exists but has an empty value, the empty value is returned, not the default. This is useful for providing fallback values for optional parameters.
Parameters:
- r: The HTTP request containing the URL with query parameters
- k: The name of the query parameter to retrieve
- d: The default value to return if the parameter doesn't exist
Returns the parameter value if it exists, otherwise the default value.
func String ¶
String reads the request body and returns it as a string. This is a convenience function that uses Bytes internally and converts the result to a string. The request body is consumed and cannot be read again.
Parameters:
- r: The HTTP request containing the body to read
Returns the request body as a string, or an error if reading fails.
func XML ¶
XML decodes XML data from the request body into a value of type T. It uses streaming XML decoding for memory efficiency and automatically closes the request body after reading. The function uses generics to provide type safety for the decoded result.
The target type T should have appropriate xml struct tags for proper unmarshaling, or implement xml.Unmarshaler interface for custom deserialization.
Parameters:
- r: The HTTP request containing XML data in the body
Returns the decoded value of type T, or an error if decoding fails.
Types ¶
This section is empty.