******************************************************************************** Response ******************************************************************************** Response converters help users to automatically convert returned type into ``response``. `Response Example `_ .. code-block:: cpp auto resp() -> awaitable { co_return response::ok() .set_header(http::field::content_type, mime::text_plain()) .set_body("Hello World!"); } auto text() -> awaitable { co_return "Hello World!"; } auto binary() -> awaitable> { co_return std::vector { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21 }; } auto variant(path_of> path) -> awaitable>> { auto [text] = path; if (text) { co_return "Hello World!"; } else { co_return std::vector { 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``. .. code-block:: cpp 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 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 { co_return my_error("You will never get anything!"); } Built-in Response Converters ================================================================================ +-------------------------+--------------------------------------------------+--------------------------------------------------------------------------------------------------------------+ | Response Converter | Description | | +=========================+==================================================+==============================================================================================================+ | ``std::string`` | Put string into ``response`` body. | ``Content-Type: text/plain`` will be inserted. | +-------------------------+--------------------------------------------------+--------------------------------------------------------------------------------------------------------------+ | ``std::vector`` | Put ``std::vector`` into ``response`` body. | ``Content-Type: application/octet-stream`` will be inserted. | +-------------------------+--------------------------------------------------+--------------------------------------------------------------------------------------------------------------+ | ``std::variant`` | Convert any of ``Ts...`` into ``response`` body. | Note that ``Ts...`` must be convertible to ``response``. | +-------------------------+--------------------------------------------------+--------------------------------------------------------------------------------------------------------------+ | ``expected`` | Convert ``T`` or ``E`` into ``response`` body. | Note that ``T`` and ``E`` must be convertible to ``response``. | +-------------------------+--------------------------------------------------+--------------------------------------------------------------------------------------------------------------+ | ``stream_file`` | Put ``stream_file`` into ``response`` body. | ``Content-Type: application/octet-stream`` will be inserted and ``Transfer-Encoding: Chunked`` will be used. | +-------------------------+--------------------------------------------------+--------------------------------------------------------------------------------------------------------------+