Request
mach::Request
Description
Section titled “Description”Represents an HTTP request.
Headers
Section titled “Headers”Primary header:
#include <mach/Request.hpp>Convenience header:
#include <mach/mach.hpp>Syntax
Section titled “Syntax”class Request {
public: [[nodiscard]] http::Method method() const noexcept;
[[nodiscard]] http::Version version() const noexcept;
[[nodiscard]] std::string_view target() const noexcept;
[[nodiscard]] const std::string& body() const noexcept;
[[nodiscard]] bool containsHeader(std::string_view name) const noexcept;
[[nodiscard]] std::optional<std::string_view> header(std::string_view name) const;
[[nodiscard]] std::string_view routeParam(std::string_view name) const;
template <typename T> [[nodiscard]] T routeParam(std::string_view name) const;
[[nodiscard]] bool containsCookie(std::string_view name) const noexcept;
[[nodiscard]] std::optional<std::string_view> cookie(std::string_view name) const;
[[nodiscard]] bool containsQuery(std::string_view name) const;
[[nodiscard]] std::optional<std::string_view> query(std::string_view name) const;
template <typename T> [[nodiscard]] std::optional<T> query(std::string_view name) const;};Member Functions
Section titled “Member Functions”| Name | Description |
|---|---|
method |
Returns the request method. |
version |
Returns the HTTP version. |
target |
Returns the request target. |
body |
Returns the request body. |
header |
Returns the value of an HTTP header, if present. |
containsHeader |
Returns whether an HTTP header exists in the request. |
routeParam |
Returns the value of a route parameter. |
routeParam<T> |
Returns the value of a route parameter converted to the specified type. |
containsCookie |
Returns whether a given cookie exists in the request. |
cookie |
Returns the value of an HTTP cookie if it exists. |
containsQuery |
Returns whether a given query parameter exists in the request. |
query |
Returns the value of a query parameter, if present. |
query<T> |
Returns the value of a query parameter converted to the specified type, if available. |
Thread Safety
Section titled “Thread Safety”Not thread-safe. Concurrent access to the same Request instance must be synchronized externally.
Ownership
Section titled “Ownership”Requestobjects are owned by the framework.- A
Requestobject remains valid only for the duration of the current request. - Do not store references or pointers to a
Requestobject beyond the request handling lifetime.
Example
Section titled “Example”class UsersController : public mach::ControllerBase {
public: static constexpr std::string_view route = "/users";
mach::Reply<std::string> get() { auto userAgent = request().header("user-agent");
return ok(userAgent); }};See Also
Section titled “See Also”| Name | Description |
|---|---|
Context |
Provides access to the current HTTP request and response. |
Method |
Represents an HTTP request method. |
Version |
Represents an HTTP version. |