Skip to content
Download

Overview

mach::results

Provides helper functions for creating common HTTP responses, such as ok(), created(), badRequest(), and notFound().

Primary header:

#include <mach/results/Results.hpp>

Convenience header:

#include <mach/mach.hpp>
namespace mach {
template <typename T>
[[nodiscard]] Reply<T> ok(T value);
[[nodiscard]] Reply<std::string> ok(const char* value);
[[nodiscard]] Reply<> ok();
template <typename T>
[[nodiscard]] Reply<T> created(T value);
[[nodiscard]] Reply<std::string> created(const char* value);
[[nodiscard]] Reply<> created();
[[nodiscard]] Reply<> noContent();
template <typename T>
[[nodiscard]] Reply<T> badRequest(T value);
[[nodiscard]] Reply<std::string> badRequest(const char* value);
template <typename T = void>
[[nodiscard]] Reply<T> badRequest();
template <typename T>
[[nodiscard]] Reply<T> unauthorized(T value);
[[nodiscard]] Reply<std::string> unauthorized(const char* value);
template <typename T = void>
[[nodiscard]] Reply<T> unauthorized();
template <typename T>
[[nodiscard]] Reply<T> forbidden(T value);
[[nodiscard]] Reply<std::string> forbidden(const char* value);
template <typename T = void>
[[nodiscard]] Reply<T> forbidden();
template <typename T>
[[nodiscard]] Reply<T> notFound(T value);
[[nodiscard]] Reply<std::string> notFound(const char* value);
template <typename T = void>
[[nodiscard]] Reply<T> notFound();
template <typename T>
[[nodiscard]] Reply<T> conflict(T value);
[[nodiscard]] Reply<std::string> conflict(const char* value);
template <typename T = void>
[[nodiscard]] Reply<T> conflict();
}
Name Description
ok Creates a 200 OK response.
created Creates a 201 Created response.
noContent Creates a 204 No Content response.
badRequest Creates a 400 Bad Request response.
unauthorized Creates a 401 Unauthorized response.
forbidden Creates a 403 Forbidden response.
notFound Creates a 404 Not Found response.
conflict Creates a 409 Conflict response.

These functions are convenience wrappers for constructing Reply objects and can be used from Minimal API handlers. Controller actions may also use the inherited helper functions provided by ControllerBase.

app.mapGet("/users/{id:int}", [] {
return mach::ok(User{1, "Alice"});
});
app.mapPost("/users", [] {
return mach::badRequest("The request is invalid.");
});
Name Description
Reply Represents the result of an HTTP endpoint.
Response Represents the HTTP response being built.