Skip to content
Download

Response

mach::Response

Represents an HTTP response.

Primary header:

#include <mach/Response.hpp>

Convenience header:

#include <mach/mach.hpp>
class Response {
public:
[[nodiscard]] http::Version version() const noexcept;
[[nodiscard]] http::StatusCode status() const noexcept;
[[nodiscard]] const std::string& body() const noexcept;
[[nodiscard]] std::optional<std::string_view> header(std::string_view name) const;
[[nodiscard]] bool containsHeader(std::string_view name) const noexcept;
void status(http::StatusCode status);
void body(std::string body) noexcept;
void setHeader(std::string_view name, std::string_view value);
[[nodiscard]] bool containsCookie(std::string_view name) const noexcept;
[[nodiscard]] std::optional<mach::http::Cookie> cookie(std::string_view name) const;
void addCookie(const mach::http::Cookie& cookie);
void addCookie(std::string name, std::string value);
void addCookie(std::string name, std::string value);
};
Name Description
version Returns the HTTP version.
status Returns the response HTTP status.
body Returns the response body.
header Returns the value of an HTTP header, if present.
containsHeader Returns whether an HTTP header exists.
status Sets the HTTP response status code.
body Sets the response body.
setHeader Sets the value of an HTTP response header.
containsCookie Returns whether a given cookie exists in the response.
cookie Returns a cookie if it exists.
addCookie Adds a cookie to the response.

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

  • Response objects are owned by the framework.
  • A Response object remains valid only for the duration of the current request.
  • Do not store references or pointers to a Response object beyond the request handling lifetime.
class UsersController : public mach::ControllerBase {
public:
static constexpr std::string_view route = "/users";
mach::Reply<std::string> get() {
response().setHeader("X-Powered-By", "Mach");
return ok("Hello, world!");
}
};
Name Description
Context Provides access to the current HTTP request and response.
StatusCode Represents an HTTP response status code.