Response#
Response converters help users to automatically convert returned type into response
.
auto resp() -> awaitable<response>
{
co_return response::ok()
.set_header(http::field::content_type, mime::text_plain())
.set_body("Hello World!");
}
auto text() -> awaitable<std::string>
{
co_return "Hello World!";
}
auto binary() -> awaitable<std::vector<std::uint8_t>>
{
co_return std::vector<std::uint8_t> { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20,
0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21 };
}
auto variant(path_of<std::tuple<bool>> path)
-> awaitable<std::variant<std::string, std::vector<std::uint8_t>>>
{
auto [text] = path;
if (text) {
co_return "Hello World!";
} else {
co_return std::vector<std::uint8_t> { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20,
0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21 };
}
}
Note
To enable response converter for custom type, implement customization point to_response_t
.
class my_error {
std::string msg_;
public:
my_error(std::string msg)
: msg_(std::move(msg))
{
}
auto message() const noexcept -> const std::string&
{
return msg_;
}
template <decay_to<my_error> Self>
friend auto tag_invoke(fitoria::web::to_response_t, Self&& self) -> response
{
return response::not_found()
.set_header(http::field::content_type, mime::text_plain())
.set_body(self.message());
}
};
auto err() -> awaitable<my_error>
{
co_return my_error("You will never get anything!");
}
Built-in Response Converters#
Response Converter |
Description |
|
---|---|---|
|
Put string into |
|
|
Put |
|
|
Convert any of |
Note that |
|
Convert |
Note that |
|
Put |
|