Skip to content
Download

Request

mach::Request

Represents an HTTP request.

Primary header:

#include <mach/Request.hpp>

Convenience header:

#include <mach/mach.hpp>
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;
};
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.

Not thread-safe. Concurrent access to the same Request instance must be synchronized externally.

  • Request objects are owned by the framework.
  • A Request object remains valid only for the duration of the current request.
  • Do not store references or pointers to a Request object beyond the request handling lifetime.
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);
}
};
Name Description
Context Provides access to the current HTTP request and response.
Method Represents an HTTP request method.
Version Represents an HTTP version.