Response
mach::Response
Description
Section titled “Description”Represents an HTTP response.
Headers
Section titled “Headers”Primary header:
#include <mach/Response.hpp>Convenience header:
#include <mach/mach.hpp>Syntax
Section titled “Syntax”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);};Member Functions
Section titled “Member Functions”| 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. |
Thread Safety
Section titled “Thread Safety”Not thread-safe. Concurrent access to the same Response instance must be synchronized externally.
Ownership
Section titled “Ownership”Responseobjects are owned by the framework.- A
Responseobject remains valid only for the duration of the current request. - Do not store references or pointers to a
Responseobject 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() { response().setHeader("X-Powered-By", "Mach");
return ok("Hello, world!"); }};See Also
Section titled “See Also”| Name | Description |
|---|---|
Context |
Provides access to the current HTTP request and response. |
StatusCode |
Represents an HTTP response status code. |