request

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrSessionNotFound = problem.Problem{
	Title:  "Session not found",
	Detail: "Unable to find the session in the request",
	Status: http.StatusInternalServerError,
}

Functions

func Bytes

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

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(r *http.Request, k string) *http.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

func CookieValue(r *http.Request, k string) string

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

func CookieValueOr(r *http.Request, k string, d string) string

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

func HasQuery(r *http.Request, k string) bool

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 JSON

func JSON[T any](r *http.Request) (value T, err error)

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 MustSession added in v0.6.0

func MustSession(r *http.Request, key any) contract.Session

func Param

func Param(r *http.Request, k string) string

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

func ParamOr(r *http.Request, k string, d string) string

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

func Query(r *http.Request, k string) string

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

func QueryOr(r *http.Request, k string, d string) string

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 Session added in v0.5.0

func Session(r *http.Request, key any) (contract.Session, bool)

func String

func String(r *http.Request) (string, error)

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

func XML[T any](r *http.Request) (value T, err error)

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.

Jump to

Keyboard shortcuts

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