<titledata-react-helmet="true">Page Not Found | Pistache</title><metadata-react-helmet="true"property="og:title"content="Page Not Found | Pistache"><metadata-react-helmet="true"property="og:url"content="https://pistache.io/404.html"><metadata-react-helmet="true"name="docusaurus_locale"content="en"><metadata-react-helmet="true"name="docusaurus_tag"content="default"><linkdata-react-helmet="true"rel="shortcut icon"href="/img/logo.png"><linkdata-react-helmet="true"rel="canonical"href="https://pistache.io/404.html"><linkdata-react-helmet="true"rel="alternate"href="https://pistache.io/404.html"hreflang="en"><linkdata-react-helmet="true"rel="alternate"href="https://pistache.io/404.html"hreflang="x-default"><linkrel="stylesheet"href="/assets/css/styles.35f9fda9.css">
<titledata-react-helmet="true">Page Not Found | Pistache</title><metadata-react-helmet="true"property="og:title"content="Page Not Found | Pistache"><metadata-react-helmet="true"property="og:url"content="https://pistache.io/404.html"><metadata-react-helmet="true"name="docusaurus_locale"content="en"><metadata-react-helmet="true"name="docusaurus_tag"content="default"><linkdata-react-helmet="true"rel="shortcut icon"href="/img/logo.png"><linkdata-react-helmet="true"rel="canonical"href="https://pistache.io/404.html"><linkdata-react-helmet="true"rel="alternate"href="https://pistache.io/404.html"hreflang="en"><linkdata-react-helmet="true"rel="alternate"href="https://pistache.io/404.html"hreflang="x-default"><linkrel="stylesheet"href="/assets/css/styles.35f9fda9.css">
<div><ahref="#main"class="skipToContent_1oUP shadow--md">Skip to main content</a></div><navclass="navbar navbar--fixed-top"><divclass="navbar__inner"><divclass="navbar__items"><buttonaria-label="Navigation bar toggle"class="navbar__toggle clean-btn"type="button"tabindex="0"><svgwidth="30"height="30"viewBox="0 0 30 30"aria-hidden="true"><pathstroke="currentColor"stroke-linecap="round"stroke-miterlimit="10"stroke-width="2"d="M4 7h22M4 15h22M4 23h22"></path></svg></button><aclass="navbar__brand"href="/"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--light_3UqQ navbar__logo"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--dark_hz6m navbar__logo"><bclass="navbar__title">Pistache</b></a><aclass="navbar__item navbar__link"href="/docs/">Docs</a></div><divclass="navbar__items navbar__items--right"><ahref="https://github.com/pistacheio/pistache"target="_blank"rel="noopener noreferrer"class="navbar__item navbar__link header-github-link"aria-label="GitHub repository"></a><divclass="react-toggle displayOnlyInLargeViewport_GrZ2 react-toggle--disabled"><divclass="react-toggle-track"role="button"tabindex="-1"><divclass="react-toggle-track-check"><spanclass="toggle_71bT">🌜</span></div><divclass="react-toggle-track-x"><spanclass="toggle_71bT">🌞</span></div><divclass="react-toggle-thumb"></div></div><inputtype="checkbox"class="react-toggle-screenreader-only"aria-label="Switch between dark and light mode"></div></div></div><divrole="presentation"class="navbar-sidebar__backdrop"></div><divclass="navbar-sidebar"><divclass="navbar-sidebar__brand"><aclass="navbar__brand"href="/"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--light_3UqQ navbar__logo"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--dark_hz6m navbar__logo"><bclass="navbar__title">Pistache</b></a></div><divclass="navbar-sidebar__items"><divclass="menu"><ulclass="menu__list"><liclass="menu__list-item"><aclass="menu__link"href="/docs/">Docs</a></li><liclass="menu__list-item"><ahref="https://github.com/pistacheio/pistache"target="_blank"rel="noopener noreferrer"class="menu__link header-github-link"aria-label="GitHub repository"></a></li></ul></div></div></div></nav><divclass="main-wrapper"><mainclass="container margin-vert--xl"><divclass="row"><divclass="col col--6 col--offset-3"><h1class="hero__title">Page Not Found</h1><p>We could not find what you were looking for.</p><p>Please contact the owner of the site that linked you to the original URL and let them know their link is broken.</p></div></div></main></div><footerclass="footer footer--dark"><divclass="container"><divclass="row footer__links"><divclass="col footer__col"><divclass="footer__title">Docs</div><ulclass="footer__items"><liclass="footer__item"><aclass="footer__link-item"href="/docs/">Quickstart</a></li><liclass="footer__item"><aclass="footer__link-item"href="/docs/http-handler/">User guide</a></li></ul></div><divclass="col footer__col"><divclass="footer__title">Community</div><ulclass="footer__items"><liclass="footer__item"><ahref="https://stackoverflow.com/questions/tagged/pistache"target="_blank"rel="noopener noreferrer"class="footer__link-item">Stack Overflow</a></li><liclass="footer__item"><ahref="https://web.libera.chat/#pistache"target="_blank"rel="noopener noreferrer"class="footer__link-item">#pistache on Libera.Chat</a></li></ul></div><divclass="col footer__col"><divclass="footer__title">More</div><ulclass="footer__items"><liclass="footer__item"><ahref="https://github.com/pistacheio/pistache"target="_blank"rel="noopener noreferrer"class="footer__link-item">GitHub</a></li></ul></div></div><divclass="footer__bottom text--center"><divclass="footer__copyright">Pistache, 2015 - 2021</div></div></div></footer></div>
<div><ahref="#main"class="skipToContent_1oUP shadow--md">Skip to main content</a></div><navclass="navbar navbar--fixed-top"><divclass="navbar__inner"><divclass="navbar__items"><buttonaria-label="Navigation bar toggle"class="navbar__toggle clean-btn"type="button"tabindex="0"><svgwidth="30"height="30"viewBox="0 0 30 30"aria-hidden="true"><pathstroke="currentColor"stroke-linecap="round"stroke-miterlimit="10"stroke-width="2"d="M4 7h22M4 15h22M4 23h22"></path></svg></button><aclass="navbar__brand"href="/"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--light_3UqQ navbar__logo"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--dark_hz6m navbar__logo"><bclass="navbar__title">Pistache</b></a><aclass="navbar__item navbar__link"href="/docs/">Docs</a></div><divclass="navbar__items navbar__items--right"><ahref="https://github.com/pistacheio/pistache"target="_blank"rel="noopener noreferrer"class="navbar__item navbar__link header-github-link"aria-label="GitHub repository"></a><divclass="react-toggle displayOnlyInLargeViewport_GrZ2 react-toggle--disabled"><divclass="react-toggle-track"role="button"tabindex="-1"><divclass="react-toggle-track-check"><spanclass="toggle_71bT">🌜</span></div><divclass="react-toggle-track-x"><spanclass="toggle_71bT">🌞</span></div><divclass="react-toggle-thumb"></div></div><inputtype="checkbox"class="react-toggle-screenreader-only"aria-label="Switch between dark and light mode"></div></div></div><divrole="presentation"class="navbar-sidebar__backdrop"></div><divclass="navbar-sidebar"><divclass="navbar-sidebar__brand"><aclass="navbar__brand"href="/"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--light_3UqQ navbar__logo"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--dark_hz6m navbar__logo"><bclass="navbar__title">Pistache</b></a></div><divclass="navbar-sidebar__items"><divclass="menu"><ulclass="menu__list"><liclass="menu__list-item"><aclass="menu__link"href="/docs/">Docs</a></li><liclass="menu__list-item"><ahref="https://github.com/pistacheio/pistache"target="_blank"rel="noopener noreferrer"class="menu__link header-github-link"aria-label="GitHub repository"></a></li></ul></div></div></div></nav><divclass="main-wrapper"><mainclass="container margin-vert--xl"><divclass="row"><divclass="col col--6 col--offset-3"><h1class="hero__title">Page Not Found</h1><p>We could not find what you were looking for.</p><p>Please contact the owner of the site that linked you to the original URL and let them know their link is broken.</p></div></div></main></div><footerclass="footer footer--dark"><divclass="container"><divclass="row footer__links"><divclass="col footer__col"><divclass="footer__title">Docs</div><ulclass="footer__items"><liclass="footer__item"><aclass="footer__link-item"href="/docs/">Quickstart</a></li><liclass="footer__item"><aclass="footer__link-item"href="/docs/http-handler/">User guide</a></li></ul></div><divclass="col footer__col"><divclass="footer__title">Community</div><ulclass="footer__items"><liclass="footer__item"><ahref="https://stackoverflow.com/questions/tagged/pistache"target="_blank"rel="noopener noreferrer"class="footer__link-item">Stack Overflow</a></li><liclass="footer__item"><ahref="https://web.libera.chat/#pistache"target="_blank"rel="noopener noreferrer"class="footer__link-item">#pistache on Libera.Chat</a></li></ul></div><divclass="col footer__col"><divclass="footer__title">More</div><ulclass="footer__items"><liclass="footer__item"><ahref="https://github.com/pistacheio/pistache"target="_blank"rel="noopener noreferrer"class="footer__link-item">GitHub</a></li></ul></div></div><divclass="footer__bottom text--center"><divclass="footer__copyright">Pistache, 2015 - 2021</div></div></div></footer></div>
<div><ahref="#main"class="skipToContent_1oUP shadow--md">Skip to main content</a></div><navclass="navbar navbar--fixed-top"><divclass="navbar__inner"><divclass="navbar__items"><buttonaria-label="Navigation bar toggle"class="navbar__toggle clean-btn"type="button"tabindex="0"><svgwidth="30"height="30"viewBox="0 0 30 30"aria-hidden="true"><pathstroke="currentColor"stroke-linecap="round"stroke-miterlimit="10"stroke-width="2"d="M4 7h22M4 15h22M4 23h22"></path></svg></button><aclass="navbar__brand"href="/"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--light_3UqQ navbar__logo"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--dark_hz6m navbar__logo"><bclass="navbar__title">Pistache</b></a><aaria-current="page"class="navbar__item navbar__link navbar__link--active"href="/docs/">Docs</a></div><divclass="navbar__items navbar__items--right"><ahref="https://github.com/pistacheio/pistache"target="_blank"rel="noopener noreferrer"class="navbar__item navbar__link header-github-link"aria-label="GitHub repository"></a><divclass="react-toggle displayOnlyInLargeViewport_GrZ2 react-toggle--disabled"><divclass="react-toggle-track"role="button"tabindex="-1"><divclass="react-toggle-track-check"><spanclass="toggle_71bT">🌜</span></div><divclass="react-toggle-track-x"><spanclass="toggle_71bT">🌞</span></div><divclass="react-toggle-thumb"></div></div><inputtype="checkbox"class="react-toggle-screenreader-only"aria-label="Switch between dark and light mode"></div></div></div><divrole="presentation"class="navbar-sidebar__backdrop"></div><divclass="navbar-sidebar"><divclass="navbar-sidebar__brand"><aclass="navbar__brand"href="/"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--light_3UqQ navbar__logo"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--dark_hz6m navbar__logo"><bclass="navbar__title">Pistache</b></a></div><divclass="navbar-sidebar__items"><divclass="menu"><ulclass="menu__list"><liclass="menu__list-item"><aaria-current="page"class="menu__link navbar__link--active"href="/docs/">Docs</a></li><liclass="menu__list-item"><ahref="https://github.com/pistacheio/pistache"target="_blank"rel="noopener noreferrer"class="menu__link header-github-link"aria-label="GitHub repository"></a></li></ul></div></div></div></nav><divclass="main-wrapper docs-wrapper doc-page"><divclass="docPage_31aa"><asideclass="docSidebarContainer_3Kbt"><divclass="sidebar_15mo"><navclass="menu menu--responsive thin-scrollbar menu_Bmed"aria-label="Sidebar navigation"><buttonaria-label="Open menu"aria-haspopup="true"class="button button--secondary button--sm menu__button"type="button"><svgclass="sidebarMenuIcon_fgN0"width="24"height="24"viewBox="0 0 30 30"aria-hidden="true"><pathstroke="currentColor"stroke-linecap="round"stroke-miterlimit="10"stroke-width="2"d="M4 7h22M4 15h22M4 23h22"></path></svg></button><ulclass="menu__list"><liclass="menu__list-item"><aaria-current="page"class="menu__link menu__link--active active"href="/docs/asynchronous-http-programming">Asynchronous HTTP programming</a></li><liclass="menu__list-item"><aclass="menu__link"href="/docs/headers">Headers</a></li><liclass="menu__list-item"><aclass="menu__link"href="/docs/http-handler">HTTP handler</a></li><liclass="menu__list-item"><aclass="menu__link"href="/docs/">Getting started</a></li><liclass="menu__list-item"><aclass="menu__link"href="/docs/rest-description">REST description</a></li><liclass="menu__list-item"><aclass="menu__link"href="/docs/routing">Routing</a></li></ul></nav></div></aside><mainclass="docMainContainer_3ufF"><divclass="container padding-top--md padding-bottom--lg docItemWrapper_3FMP"><divclass="row"><divclass="col docItemCol_3FnS"><divclass="docItemContainer_33ec"><article><divclass="markdown"><header><h1class="h1Heading_27L5">Asynchronous HTTP programming</h1></header><p>Interfaces provided by Pistaches are <em>asynchronous</em> and <em>non-blocking</em>. Asynchronous programming allows for code to continue executing even if the result of a given call is not available yet. Calls that provide an asynchronous interface are referred to <em>asynchronous calls</em>.</p><p>An example of such a call is the <code>send()</code> function provided by the <code>ResponseWriter</code> interface. This function returns the number of bytes written to the socket file descriptor associated to the connection. However, instead of returning directly the value to the caller and thus blocking the caller, it wraps the value into a component called a <code>Promise</code>.</p><p>A <code>Promise</code> is the Pistache’s implementation of the <ahref="https://promisesaplus.com"target="_blank"rel="noopener noreferrer">Promises/A+</a> standard available in many JavaScript implementations. Simply put, during an asynchronous call, a <code>Promise</code> separates the launch of an asynchronous operation from the retrieval of its result. While the asynchronous might still be running, a <code>Promise<T></code> is directly returned to the caller to retrieve the final result when it becomes available. A so called continuation can be attach to a <code>Promise</code> to execute a callback when the result becomes available (when the <code>Promise</code> has been resolved or fulfilled).</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token keyword"style="font-style:italic">auto</span><spanclass="token plain"> res </span><spanclass="token operator"style="color:rgb(137, 221, 255)">=</span><spanclass="token plain"> response</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token function"style="color:rgb(130, 170, 255)">send</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain">Http</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Code</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Ok</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span><spanclass="token string"style="color:rgb(195, 232, 141)">"Hello World"</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">res</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token function"style="color:rgb(130, 170, 255)">then</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">[</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">]</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain">ssize_t bytes</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">{</span><spanclass="token plain"> std</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">cout </span><spanclass="token operator"style="color:rgb(137, 221, 255)"><<</span><spanclass="token plain"> bytes </span><spanclass="token operator"style="color:rgb(137, 221, 255)"><<</span><spanclass="token plain"></span><spanclass="token string"style="color:rgb(195, 232, 141)">" bytes have been sent\n"</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">}</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> Async</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">NoExcept</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>The <code>then()</code> member is used to attach a callback to the <code>Promise</code>. The first argument is a callable that will be called when the <code>Promise</code> has been <strong>succesfully</strong> resolved. If, for some reason, an error occurs during the asynchronous operation, a <code>Promise</code> can be <strong>rejected</strong> and will then fail. In this case, the second callable will be called. <code>Async::NoExcept</code> is a special callback that will call <ahref="https://en.cppreference.com/w/cpp/error/terminate"target="_blank"rel="noopener noreferrer"><code>std::terminate()</code></a> if the promise failed. This is the equivalent of the <code>noexcept</code> keyword.</p><p>Other generic callbacks can also be used in this case:</p><ul><li><code>Async::IgnoreException</code> will simply ignore the exception and let the program continue</li><li><code>Async::Throw</code> will "rethrow" the exception up to an eventual promise call-chain. This has the same effect than the <code>throw</code> keyword, except that it is suitable for promises</li></ul><p>Exceptions in promises callbacks are propagated through an <code>exception_ptr</code>. Promises can also be chained together to create a whole asynchronous pipeline:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token keyword"style="font-style:italic">auto</span><spanclass="token plain"> fetchOp </span><spanclass="token operator"style="color:rgb(137, 221, 255)">=</span><spanclass="token plain"></span><spanclass="token function"style="color:rgb(130, 170, 255)">fetchDatabase</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">fetchOp</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token function"style="color:rgb(130, 170, 255)">then</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">[</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">]</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token keyword"style="font-style:italic">const</span><spanclass="token plain"> User</span><spanclass="token operator"style="color:rgb(137, 221, 255)">&</span><spanclass="token plain"> user</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">{</span><spanclass="token plain"></span><spanclass="token keyword"style="font-style:italic">return</span><spanclass="token plain"></span><spanclass="token function"style="color:rgb(130, 170, 255)">fetchUserInfo</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain">user</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">}</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> Async</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Throw</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token function"style="color:rgb(130, 170, 255)">then</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">[</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">]</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token keyword"style="font-style:italic">const</span><spanclass="token plain"> UserInfo</span><spanclass="token operator"style="color:rgb(137, 221, 255)">&</span><spanclass="token plain"> info</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">{</span><spanclass="token plain"> std</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">cout </span><spanclass="token operator"style="color:rgb(137, 221, 255)"><<</span><spanclass="token plain"></span><spanclass="token string"style="color:rgb(195, 232, 141)">"User name = "</span><spanclass="token plain"></span><spanclass="token operator"style="color:rgb(137, 221, 255)"><<</span><spanclass="token plain"> info</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token plain">name </span><spanclass="token operator"style="color:rgb(137, 221, 255)"><<</span><spanclass="token plain"></span><spanclass="token string"style="color:rgb(195, 232, 141)">'\n'</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">}</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">[</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">]</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain">exception_ptr ptr</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">{</span><spanclass="token plain"> std</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">cerr </span><spanclass="token operator"style="color:rgb(137, 221, 255)"><<</span><spanclass="token plain"></span><spanclass="token string"style="color:rgb(195, 232, 141)">"An exception occured during user retrieval\n"</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">}</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>Line 5 will propagate the exception if <code>fetchDatabase()</code> failed and rejected the promise.</p></div><footerclass="row docusaurus-mt-lg"><divclass="col"><ahref="https://github.com/pistacheio/pistache/edit/master/pistache.io/docs/asynchronous-http-programming.md"target="_blank"rel="noreferrer noopener"><svgfill="currentColor"height="20"width="20"viewBox="0 0 40 40"class="iconEdit_2_ui"aria-hidden="true"><g><pathd="m34.5 11.7l-3 3.1-6.3-6.3 3.1-3q0.5-0.5 1.2-0.5t1.1 0.5l3.9 3.9q0.5 0.4 0.5 1.1t-0.5 1.2z m-29.5 17.1l18.4-18.5 6.3 6.3-18.4 18.4h-6.3v-6.2z"></path></g></svg>Edit this page</a></div><divclass="col lastUpdated_3DPF"></div></footer></article><navclass="pagination-nav docusaurus-mt-lg"aria-label="Docs pages navigation"><divclass="pagination-nav__item"></div><divclass="pagination-nav__item pagination-nav__item--next"><aclass="pagination-nav__link"href="/docs/headers"><divclass="pagination-nav__sublabel">Next</div><divclass="pagination-nav__label">Headers »</div></a></div></nav></div></div><divclass="col col--3"><divclass="tableOfContents_35-E thin-scrollbar"></div></div></div></div></main></div></div><footerclass="footer footer--dark"><divclass="container"><divclass="row footer__links"><divclass="col footer__col"><divclass="footer__title">Docs</div><ulclass="footer__items"><liclass="footer__item"><aclass="footer__link-item"href="/docs/">Quickstart</a></li><liclass="footer__item"><aclass="footer__link-item"href="/docs/http-handler/">User guide</a></li></ul></div><divclass="col footer__col"><divclass="footer__title">Community</div><ulclass="footer__items"><liclass="footer__item"><ahref="https://stackoverflow.com/questions/tagged/pistache"target="_blank"rel="noopener noreferrer"class="footer__link-item">Stack Overflow</a></li><liclass="footer__item"><ahref="https://web.libera.chat/#pistache"target="_blank"rel="noopener noreferrer"class="footer__link-item">#pistache on Libera.Chat</a></li></ul></div><divclass="col footer__col"><divclass="footer__title">More</div><ulclass="footer__items"><liclass="footer__item"><ahref="https://github.com/pistacheio/pistache"target="_blank"rel="noopener noreferrer"class="footer__link-item">GitHub</a></li></ul></div></div><divclass="footer__bottom text--center"><divclass="footer__copyright">Pistache, 2015 - 2021</div></div></div></footer></div>
<div><ahref="#main"class="skipToContent_1oUP shadow--md">Skip to main content</a></div><navclass="navbar navbar--fixed-top"><divclass="navbar__inner"><divclass="navbar__items"><buttonaria-label="Navigation bar toggle"class="navbar__toggle clean-btn"type="button"tabindex="0"><svgwidth="30"height="30"viewBox="0 0 30 30"aria-hidden="true"><pathstroke="currentColor"stroke-linecap="round"stroke-miterlimit="10"stroke-width="2"d="M4 7h22M4 15h22M4 23h22"></path></svg></button><aclass="navbar__brand"href="/"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--light_3UqQ navbar__logo"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--dark_hz6m navbar__logo"><bclass="navbar__title">Pistache</b></a><aaria-current="page"class="navbar__item navbar__link navbar__link--active"href="/docs/">Docs</a></div><divclass="navbar__items navbar__items--right"><ahref="https://github.com/pistacheio/pistache"target="_blank"rel="noopener noreferrer"class="navbar__item navbar__link header-github-link"aria-label="GitHub repository"></a><divclass="react-toggle displayOnlyInLargeViewport_GrZ2 react-toggle--disabled"><divclass="react-toggle-track"role="button"tabindex="-1"><divclass="react-toggle-track-check"><spanclass="toggle_71bT">🌜</span></div><divclass="react-toggle-track-x"><spanclass="toggle_71bT">🌞</span></div><divclass="react-toggle-thumb"></div></div><inputtype="checkbox"class="react-toggle-screenreader-only"aria-label="Switch between dark and light mode"></div></div></div><divrole="presentation"class="navbar-sidebar__backdrop"></div><divclass="navbar-sidebar"><divclass="navbar-sidebar__brand"><aclass="navbar__brand"href="/"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--light_3UqQ navbar__logo"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--dark_hz6m navbar__logo"><bclass="navbar__title">Pistache</b></a></div><divclass="navbar-sidebar__items"><divclass="menu"><ulclass="menu__list"><liclass="menu__list-item"><aaria-current="page"class="menu__link navbar__link--active"href="/docs/">Docs</a></li><liclass="menu__list-item"><ahref="https://github.com/pistacheio/pistache"target="_blank"rel="noopener noreferrer"class="menu__link header-github-link"aria-label="GitHub repository"></a></li></ul></div></div></div></nav><divclass="main-wrapper docs-wrapper doc-page"><divclass="docPage_31aa"><asideclass="docSidebarContainer_3Kbt"><divclass="sidebar_15mo"><navclass="menu menu--responsive thin-scrollbar menu_Bmed"aria-label="Sidebar navigation"><buttonaria-label="Open menu"aria-haspopup="true"class="button button--secondary button--sm menu__button"type="button"><svgclass="sidebarMenuIcon_fgN0"width="24"height="24"viewBox="0 0 30 30"aria-hidden="true"><pathstroke="currentColor"stroke-linecap="round"stroke-miterlimit="10"stroke-width="2"d="M4 7h22M4 15h22M4 23h22"></path></svg></button><ulclass="menu__list"><liclass="menu__list-item"><aaria-current="page"class="menu__link menu__link--active active"href="/docs/asynchronous-http-programming">Asynchronous HTTP programming</a></li><liclass="menu__list-item"><aclass="menu__link"href="/docs/headers">Headers</a></li><liclass="menu__list-item"><aclass="menu__link"href="/docs/http-handler">HTTP handler</a></li><liclass="menu__list-item"><aclass="menu__link"href="/docs/">Getting started</a></li><liclass="menu__list-item"><aclass="menu__link"href="/docs/rest-description">REST description</a></li><liclass="menu__list-item"><aclass="menu__link"href="/docs/routing">Routing</a></li></ul></nav></div></aside><mainclass="docMainContainer_3ufF"><divclass="container padding-top--md padding-bottom--lg docItemWrapper_3FMP"><divclass="row"><divclass="col docItemCol_3FnS"><divclass="docItemContainer_33ec"><article><divclass="markdown"><header><h1class="h1Heading_27L5">Asynchronous HTTP programming</h1></header><p>Interfaces provided by Pistaches are <em>asynchronous</em> and <em>non-blocking</em>. Asynchronous programming allows for code to continue executing even if the result of a given call is not available yet. Calls that provide an asynchronous interface are referred to <em>asynchronous calls</em>.</p><p>An example of such a call is the <code>send()</code> function provided by the <code>ResponseWriter</code> interface. This function returns the number of bytes written to the socket file descriptor associated to the connection. However, instead of returning directly the value to the caller and thus blocking the caller, it wraps the value into a component called a <code>Promise</code>.</p><p>A <code>Promise</code> is the Pistache’s implementation of the <ahref="https://promisesaplus.com"target="_blank"rel="noopener noreferrer">Promises/A+</a> standard available in many JavaScript implementations. Simply put, during an asynchronous call, a <code>Promise</code> separates the launch of an asynchronous operation from the retrieval of its result. While the asynchronous might still be running, a <code>Promise<T></code> is directly returned to the caller to retrieve the final result when it becomes available. A so called continuation can be attach to a <code>Promise</code> to execute a callback when the result becomes available (when the <code>Promise</code> has been resolved or fulfilled).</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token keyword"style="font-style:italic">auto</span><spanclass="token plain"> res </span><spanclass="token operator"style="color:rgb(137, 221, 255)">=</span><spanclass="token plain"> response</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token function"style="color:rgb(130, 170, 255)">send</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain">Http</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Code</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Ok</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span><spanclass="token string"style="color:rgb(195, 232, 141)">"Hello World"</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">res</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token function"style="color:rgb(130, 170, 255)">then</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">[</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">]</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain">ssize_t bytes</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">{</span><spanclass="token plain"> std</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">cout </span><spanclass="token operator"style="color:rgb(137, 221, 255)"><<</span><spanclass="token plain"> bytes </span><spanclass="token operator"style="color:rgb(137, 221, 255)"><<</span><spanclass="token plain"></span><spanclass="token string"style="color:rgb(195, 232, 141)">" bytes have been sent\n"</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">}</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> Async</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">NoExcept</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>The <code>then()</code> member is used to attach a callback to the <code>Promise</code>. The first argument is a callable that will be called when the <code>Promise</code> has been <strong>succesfully</strong> resolved. If, for some reason, an error occurs during the asynchronous operation, a <code>Promise</code> can be <strong>rejected</strong> and will then fail. In this case, the second callable will be called. <code>Async::NoExcept</code> is a special callback that will call <ahref="https://en.cppreference.com/w/cpp/error/terminate"target="_blank"rel="noopener noreferrer"><code>std::terminate()</code></a> if the promise failed. This is the equivalent of the <code>noexcept</code> keyword.</p><p>Other generic callbacks can also be used in this case:</p><ul><li><code>Async::IgnoreException</code> will simply ignore the exception and let the program continue</li><li><code>Async::Throw</code> will "rethrow" the exception up to an eventual promise call-chain. This has the same effect than the <code>throw</code> keyword, except that it is suitable for promises</li></ul><p>Exceptions in promises callbacks are propagated through an <code>exception_ptr</code>. Promises can also be chained together to create a whole asynchronous pipeline:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token keyword"style="font-style:italic">auto</span><spanclass="token plain"> fetchOp </span><spanclass="token operator"style="color:rgb(137, 221, 255)">=</span><spanclass="token plain"></span><spanclass="token function"style="color:rgb(130, 170, 255)">fetchDatabase</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">fetchOp</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token function"style="color:rgb(130, 170, 255)">then</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">[</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">]</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token keyword"style="font-style:italic">const</span><spanclass="token plain"> User</span><spanclass="token operator"style="color:rgb(137, 221, 255)">&</span><spanclass="token plain"> user</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">{</span><spanclass="token plain"></span><spanclass="token keyword"style="font-style:italic">return</span><spanclass="token plain"></span><spanclass="token function"style="color:rgb(130, 170, 255)">fetchUserInfo</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain">user</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">}</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> Async</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Throw</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token function"style="color:rgb(130, 170, 255)">then</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">[</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">]</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token keyword"style="font-style:italic">const</span><spanclass="token plain"> UserInfo</span><spanclass="token operator"style="color:rgb(137, 221, 255)">&</span><spanclass="token plain"> info</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">{</span><spanclass="token plain"> std</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">cout </span><spanclass="token operator"style="color:rgb(137, 221, 255)"><<</span><spanclass="token plain"></span><spanclass="token string"style="color:rgb(195, 232, 141)">"User name = "</span><spanclass="token plain"></span><spanclass="token operator"style="color:rgb(137, 221, 255)"><<</span><spanclass="token plain"> info</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token plain">name </span><spanclass="token operator"style="color:rgb(137, 221, 255)"><<</span><spanclass="token plain"></span><spanclass="token string"style="color:rgb(195, 232, 141)">'\n'</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">}</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">[</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">]</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain">exception_ptr ptr</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">{</span><spanclass="token plain"> std</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">cerr </span><spanclass="token operator"style="color:rgb(137, 221, 255)"><<</span><spanclass="token plain"></span><spanclass="token string"style="color:rgb(195, 232, 141)">"An exception occured during user retrieval\n"</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">}</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>Line 5 will propagate the exception if <code>fetchDatabase()</code> failed and rejected the promise.</p></div><footerclass="row docusaurus-mt-lg"><divclass="col"><ahref="https://github.com/pistacheio/pistache/edit/master/pistache.io/docs/asynchronous-http-programming.md"target="_blank"rel="noreferrer noopener"><svgfill="currentColor"height="20"width="20"viewBox="0 0 40 40"class="iconEdit_2_ui"aria-hidden="true"><g><pathd="m34.5 11.7l-3 3.1-6.3-6.3 3.1-3q0.5-0.5 1.2-0.5t1.1 0.5l3.9 3.9q0.5 0.4 0.5 1.1t-0.5 1.2z m-29.5 17.1l18.4-18.5 6.3 6.3-18.4 18.4h-6.3v-6.2z"></path></g></svg>Edit this page</a></div><divclass="col lastUpdated_3DPF"></div></footer></article><navclass="pagination-nav docusaurus-mt-lg"aria-label="Docs pages navigation"><divclass="pagination-nav__item"></div><divclass="pagination-nav__item pagination-nav__item--next"><aclass="pagination-nav__link"href="/docs/headers"><divclass="pagination-nav__sublabel">Next</div><divclass="pagination-nav__label">Headers »</div></a></div></nav></div></div><divclass="col col--3"><divclass="tableOfContents_35-E thin-scrollbar"></div></div></div></div></main></div></div><footerclass="footer footer--dark"><divclass="container"><divclass="row footer__links"><divclass="col footer__col"><divclass="footer__title">Docs</div><ulclass="footer__items"><liclass="footer__item"><aclass="footer__link-item"href="/docs/">Quickstart</a></li><liclass="footer__item"><aclass="footer__link-item"href="/docs/http-handler/">User guide</a></li></ul></div><divclass="col footer__col"><divclass="footer__title">Community</div><ulclass="footer__items"><liclass="footer__item"><ahref="https://stackoverflow.com/questions/tagged/pistache"target="_blank"rel="noopener noreferrer"class="footer__link-item">Stack Overflow</a></li><liclass="footer__item"><ahref="https://web.libera.chat/#pistache"target="_blank"rel="noopener noreferrer"class="footer__link-item">#pistache on Libera.Chat</a></li></ul></div><divclass="col footer__col"><divclass="footer__title">More</div><ulclass="footer__items"><liclass="footer__item"><ahref="https://github.com/pistacheio/pistache"target="_blank"rel="noopener noreferrer"class="footer__link-item">GitHub</a></li></ul></div></div><divclass="footer__bottom text--center"><divclass="footer__copyright">Pistache, 2015 - 2021</div></div></div></footer></div>
<div><ahref="#main"class="skipToContent_1oUP shadow--md">Skip to main content</a></div><navclass="navbar navbar--fixed-top"><divclass="navbar__inner"><divclass="navbar__items"><buttonaria-label="Navigation bar toggle"class="navbar__toggle clean-btn"type="button"tabindex="0"><svgwidth="30"height="30"viewBox="0 0 30 30"aria-hidden="true"><pathstroke="currentColor"stroke-linecap="round"stroke-miterlimit="10"stroke-width="2"d="M4 7h22M4 15h22M4 23h22"></path></svg></button><aclass="navbar__brand"href="/"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--light_3UqQ navbar__logo"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--dark_hz6m navbar__logo"><bclass="navbar__title">Pistache</b></a><aaria-current="page"class="navbar__item navbar__link navbar__link--active"href="/docs/">Docs</a></div><divclass="navbar__items navbar__items--right"><ahref="https://github.com/pistacheio/pistache"target="_blank"rel="noopener noreferrer"class="navbar__item navbar__link header-github-link"aria-label="GitHub repository"></a><divclass="react-toggle displayOnlyInLargeViewport_GrZ2 react-toggle--disabled"><divclass="react-toggle-track"role="button"tabindex="-1"><divclass="react-toggle-track-check"><spanclass="toggle_71bT">🌜</span></div><divclass="react-toggle-track-x"><spanclass="toggle_71bT">🌞</span></div><divclass="react-toggle-thumb"></div></div><inputtype="checkbox"class="react-toggle-screenreader-only"aria-label="Switch between dark and light mode"></div></div></div><divrole="presentation"class="navbar-sidebar__backdrop"></div><divclass="navbar-sidebar"><divclass="navbar-sidebar__brand"><aclass="navbar__brand"href="/"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--light_3UqQ navbar__logo"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--dark_hz6m navbar__logo"><bclass="navbar__title">Pistache</b></a></div><divclass="navbar-sidebar__items"><divclass="menu"><ulclass="menu__list"><liclass="menu__list-item"><aaria-current="page"class="menu__link navbar__link--active"href="/docs/">Docs</a></li><liclass="menu__list-item"><ahref="https://github.com/pistacheio/pistache"target="_blank"rel="noopener noreferrer"class="menu__link header-github-link"aria-label="GitHub repository"></a></li></ul></div></div></div></nav><divclass="main-wrapper docs-wrapper doc-page"><divclass="docPage_31aa"><asideclass="docSidebarContainer_3Kbt"><divclass="sidebar_15mo"><navclass="menu menu--responsive thin-scrollbar menu_Bmed"aria-label="Sidebar navigation"><buttonaria-label="Open menu"aria-haspopup="true"class="button button--secondary button--sm menu__button"type="button"><svgclass="sidebarMenuIcon_fgN0"width="24"height="24"viewBox="0 0 30 30"aria-hidden="true"><pathstroke="currentColor"stroke-linecap="round"stroke-miterlimit="10"stroke-width="2"d="M4 7h22M4 15h22M4 23h22"></path></svg></button><ulclass="menu__list"><liclass="menu__list-item"><aclass="menu__link"href="/docs/asynchronous-http-programming">Asynchronous HTTP programming</a></li><liclass="menu__list-item"><aclass="menu__link"href="/docs/headers">Headers</a></li><liclass="menu__list-item"><aaria-current="page"class="menu__link menu__link--active active"href="/docs/http-handler">HTTP handler</a></li><liclass="menu__list-item"><aclass="menu__link"href="/docs/">Getting started</a></li><liclass="menu__list-item"><aclass="menu__link"href="/docs/rest-description">REST description</a></li><liclass="menu__list-item"><aclass="menu__link"href="/docs/routing">Routing</a></li></ul></nav></div></aside><mainclass="docMainContainer_3ufF"><divclass="container padding-top--md padding-bottom--lg docItemWrapper_3FMP"><divclass="row"><divclass="col docItemCol_3FnS"><divclass="docItemContainer_33ec"><article><divclass="markdown"><header><h1class="h1Heading_27L5">HTTP handler</h1></header><p>Requests that are received by Pistache are handled by a special class called <code>Http::Handler</code>. This class declares a bunch of virtual methods that can be overriden to handle special events that occur on the socket and/or connection.</p><p>The <code>onRequest()</code> function must be overriden. This function is called whenever Pistache received data and correctly parsed it as an HTTP request.</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token keyword"style="font-style:italic">virtual</span><spanclass="token plain"></span><spanclass="token keyword"style="font-style:italic">void</span><spanclass="token plain"></span><spanclass="token function"style="color:rgb(130, 170, 255)">onRequest</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token keyword"style="font-style:italic">const</span><spanclass="token plain"> Http</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Request</span><spanclass="token operator"style="color:rgb(137, 221, 255)">&</span><spanclass="token plain"> request</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"> Http</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">ResponseWriter response</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>The first argument is an object of type <code>Http::Request</code> representing the request itself. It contains a bunch of informations including:</p><ul><li>The resource associated to the request</li><li>The query parameters</li><li>The headers</li><li>The body of the request</li></ul><p>The <code>Request</code> object gives a read-only access to these informations. You can access them through a couple of getters but can not modify them. An HTTP request is <strong>immutable</strong>.</p><h2><aaria-hidden="true"tabindex="-1"class="anchor enhancedAnchor_2LWZ"id="sending-a-response"></a>Sending a response<aclass="hash-link"href="#sending-a-response"title="Direct link to heading">#</a></h2><p><code>ResponseWriter</code> is an object from which the final HTTP response is sent to the client. The <code>onRequest()</code> function does not return anything (<code>void</code>). Instead, the response is sent through the <code>ResponseWriter</code> class. This class provides a bunch of <code>send()</code> function overloads to send the response:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">Async</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Promise</span><spanclass="token operator"style="color:rgb(137, 221, 255)"><</span><spanclass="token plain">ssize_t</span><spanclass="token operator"style="color:rgb(137, 221, 255)">></span><spanclass="token plain"></span><spanclass="token function"style="color:rgb(130, 170, 255)">send</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain">Code code</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>You can use this overload to send a response with an empty body and a given HTTP Code (e.g <code>Http::Code::Ok</code>)</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">Async</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Promise</span><spanclass="token operator"style="color:rgb(137, 221, 255)"><</span><spanclass="token plain">ssize_t</span><spanclass="token operator"style="color:rgb(137, 221, 255)">></span><spanclass="token plain"></span><spanclass="token function"style="color:rgb(130, 170, 255)">send</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> Code code</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token keyword"style="font-style:italic">const</span><spanclass="token plain"> std</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">string</span><spanclass="token operator"style="color:rgb(137, 221, 255)">&</span><spanclass="token plain"> body</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token keyword"style="font-style:italic">const</span><spanclass="token plain"> Mime</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">MediaType </span><spanclass="token operator"style="color:rgb(137, 221, 255)">&</span><spanclass="token plain">mime </span><spanclass="token operator"style="color:rgb(137, 221, 255)">=</span><spanclass="token plain"></span><spanclass="token class-name"style="color:rgb(255, 203, 107)">Mime</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token function"style="color:rgb(130, 170, 255)">MediaType</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>This overload can be used to send a response with static, fixed-size content (body). A MIME type can also be specified, which will be sent through the <code>Content-Type</code> header.</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token keyword"style="font-style:italic">template</span><spanclass="token operator"style="color:rgb(137, 221, 255)"><</span><spanclass="token plain">size_t N</span><spanclass="token operator"style="color:rgb(137, 221, 255)">></span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">Async</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Promise</span><spanclass="token operator"style="color:rgb(137, 221, 255)"><</span><spanclass="token plain">ssize_t</span><spanclass="token operator"style="color:rgb(137, 221, 255)">></span><spanclass="token plain"></span><spanclass="token function"style="color:rgb(130, 170, 255)">send</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> Code code</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token keyword"style="font-style:italic">const</span><spanclass="token plain"></span><spanclass="token keyword"style="font-style:italic">char</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token operator"style="color:rgb(137, 221, 255)">&</span><spanclass="token plain">arr</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">[</span><spanclass="token plain">N</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">]</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token keyword"style="font-style:italic">const</span><spanclass="token plain"> Mime</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">MediaType</span><spanclass="token operator"style="color:rgb(137, 221, 255)">&</span><spanclass="token plain"> mime </span><spanclass="token operator"style="color:rgb(137, 221, 255)">=</span><spanclass="token plain"></span><spanclass="token class-name"style="color:rgb(255, 203, 107)">Mime</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token function"style="color:rgb(130, 170, 255)">MediaType</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>This version can also be used to send a fixed-size response with a body except that it does not need to construct a string (no memory is allocated). The size of the content is directly deduced by the compiler. This version only works with raw string literals.</p><p>These functions are asynchronous, meaning that they do not return a plain old <code>ssize_t</code> value indicating the number of bytes being sent, but instead a <code>Promise</code> that will be fulfilled later on. See the next section for more details on asynchronous programming with Pistache.</p><h2><aaria-hidden="true"tabindex="-1"class="anchor enhancedAnchor_2LWZ"id="response-streaming"></a>Response streaming<aclass="hash-link"href="#response-streaming"title="Direct link to heading">#</a></h2><p>Sometimes, content that is to be sent back to the user can not be known in advance, thus the length can not be determined in advance. For that matter, the HTTP specification defines a special data-transfer mechanism called <ahref="https://tools.ietf.org/html/rfc7230#section-4.1"target="_blank"rel="noopener noreferrer">chunked encoding</a> where data is sent in a series of <em>chunks</em>. This mechanism uses the <code>Transfer-Encoding</code> HTTP header in place of the <code>Content-Length</code> one.</p><p>To stream content, Pistache provides a special <code>ResponseStream</code> class. To get a <code>ResponseStream</code> from a <code>ResponseWriter</code>, call the <code>stream()</code> member function:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token keyword"style="font-style:italic">auto</span><spanclass="token plain"> stream </span><spanclass="token operator"style="color:rgb(137, 221, 255)">=</span><spanclass="token plain"> response</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token function"style="color:rgb(130, 170, 255)">stream</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain">Http</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Code</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Ok</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>To initate a stream, you have to pass the HTTP status code to the stream function (here <code>Http::Code::Ok</code> or <code>HTTP 200</code>). The <code>ResponseStream</code> class provides an <code>iostream</code> like interface that overloads the <code><<</code> operator.</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">stream </span><spanclass="token operator"style="color:rgb(137, 221, 255)"><<</span><spanclass="token plain"></span><spanclass="token string"style="color:rgb(195, 232, 141)">"PO"</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">stream </span><spanclass="token operator"style="color:rgb(137, 221, 255)"><<</span><spanclass="token plain"></span><spanclass="token string"style="color:rgb(195, 232, 141)">"NG"</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>The first line will write a chunk of size 2 with the content <em>PO</em> to the stream's buffer. The second line will write a second chunk of size 2 with the content <em>NG</em>. To end the stream and flush the content, use the special <code>ends</code> marker:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">stream </span><spanclass="token operator"style="color:rgb(137, 221, 255)"><<</span><spanclass="token plain"> ends</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>The <code>ends</code> marker will write the last chunk of size 0 and send the final data over the network. To simply flush the stream's buffer without ending the stream, you can use the <code>flush</code> marker:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">stream </span><spanclass="token operator"style="color:rgb(137, 221, 255)"><<</span><spanclass="token plain"> flush</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><divclass="admonition admonition-caution alert alert--warning"><divclass="admonition-heading"><h5><spanclass="admonition-icon"><svgxmlns="http://www.w3.org/2000/svg"width="16"height="16"viewBox="0 0 16 16"><pathfill-rule="evenodd"d="M8.893 1.5c-.183-.31-.52-.5-.887-.5s-.703.19-.886.5L.138 13.499a.98.98 0 0 0 0 1.001c.193.31.53.501.886.501h13.964c.367 0 .704-.19.877-.5a1.03 1.03 0 0 0 .01-1.002L8.893 1.5zm.133 11.497H6.987v-2.003h2.039v2.003zm0-3.004H6.987V5.987h2.039v4.006z"></path></svg></span>Headers writing</h5></div><divclass="admonition-content"><p>After starting a stream, headers become immutable. They must be written to the response before creating a <code>ResponseStream</code>:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">response</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token function"style="color:rgb(130, 170, 255)">headers</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token plain">add</span><spanclass="token operator"style="color:rgb(137, 221, 255)"><</span><spanclass="token plain">Header</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Server</span><spanclass="token operator"style="color:rgb(137, 221, 255)">></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token string"style="color:rgb(195, 232, 141)">"lys"</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token plain">add</span><spanclass="token operator"style="color:rgb(137, 221, 255)"><</span><spanclass="token plain">Header</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">ContentType</span><spanclass="token operator"style="color:rgb(137, 221, 255)">></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token function"style="color:rgb(130, 170, 255)">MIME</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain">Text</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"> Plain</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"style="display:inline-block">
<div><ahref="#main"class="skipToContent_1oUP shadow--md">Skip to main content</a></div><navclass="navbar navbar--fixed-top"><divclass="navbar__inner"><divclass="navbar__items"><buttonaria-label="Navigation bar toggle"class="navbar__toggle clean-btn"type="button"tabindex="0"><svgwidth="30"height="30"viewBox="0 0 30 30"aria-hidden="true"><pathstroke="currentColor"stroke-linecap="round"stroke-miterlimit="10"stroke-width="2"d="M4 7h22M4 15h22M4 23h22"></path></svg></button><aclass="navbar__brand"href="/"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--light_3UqQ navbar__logo"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--dark_hz6m navbar__logo"><bclass="navbar__title">Pistache</b></a><aaria-current="page"class="navbar__item navbar__link navbar__link--active"href="/docs/">Docs</a></div><divclass="navbar__items navbar__items--right"><ahref="https://github.com/pistacheio/pistache"target="_blank"rel="noopener noreferrer"class="navbar__item navbar__link header-github-link"aria-label="GitHub repository"></a><divclass="react-toggle displayOnlyInLargeViewport_GrZ2 react-toggle--disabled"><divclass="react-toggle-track"role="button"tabindex="-1"><divclass="react-toggle-track-check"><spanclass="toggle_71bT">🌜</span></div><divclass="react-toggle-track-x"><spanclass="toggle_71bT">🌞</span></div><divclass="react-toggle-thumb"></div></div><inputtype="checkbox"class="react-toggle-screenreader-only"aria-label="Switch between dark and light mode"></div></div></div><divrole="presentation"class="navbar-sidebar__backdrop"></div><divclass="navbar-sidebar"><divclass="navbar-sidebar__brand"><aclass="navbar__brand"href="/"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--light_3UqQ navbar__logo"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--dark_hz6m navbar__logo"><bclass="navbar__title">Pistache</b></a></div><divclass="navbar-sidebar__items"><divclass="menu"><ulclass="menu__list"><liclass="menu__list-item"><aaria-current="page"class="menu__link navbar__link--active"href="/docs/">Docs</a></li><liclass="menu__list-item"><ahref="https://github.com/pistacheio/pistache"target="_blank"rel="noopener noreferrer"class="menu__link header-github-link"aria-label="GitHub repository"></a></li></ul></div></div></div></nav><divclass="main-wrapper docs-wrapper doc-page"><divclass="docPage_31aa"><asideclass="docSidebarContainer_3Kbt"><divclass="sidebar_15mo"><navclass="menu menu--responsive thin-scrollbar menu_Bmed"aria-label="Sidebar navigation"><buttonaria-label="Open menu"aria-haspopup="true"class="button button--secondary button--sm menu__button"type="button"><svgclass="sidebarMenuIcon_fgN0"width="24"height="24"viewBox="0 0 30 30"aria-hidden="true"><pathstroke="currentColor"stroke-linecap="round"stroke-miterlimit="10"stroke-width="2"d="M4 7h22M4 15h22M4 23h22"></path></svg></button><ulclass="menu__list"><liclass="menu__list-item"><aclass="menu__link"href="/docs/asynchronous-http-programming">Asynchronous HTTP programming</a></li><liclass="menu__list-item"><aclass="menu__link"href="/docs/headers">Headers</a></li><liclass="menu__list-item"><aaria-current="page"class="menu__link menu__link--active active"href="/docs/http-handler">HTTP handler</a></li><liclass="menu__list-item"><aclass="menu__link"href="/docs/">Getting started</a></li><liclass="menu__list-item"><aclass="menu__link"href="/docs/rest-description">REST description</a></li><liclass="menu__list-item"><aclass="menu__link"href="/docs/routing">Routing</a></li></ul></nav></div></aside><mainclass="docMainContainer_3ufF"><divclass="container padding-top--md padding-bottom--lg docItemWrapper_3FMP"><divclass="row"><divclass="col docItemCol_3FnS"><divclass="docItemContainer_33ec"><article><divclass="markdown"><header><h1class="h1Heading_27L5">HTTP handler</h1></header><p>Requests that are received by Pistache are handled by a special class called <code>Http::Handler</code>. This class declares a bunch of virtual methods that can be overriden to handle special events that occur on the socket and/or connection.</p><p>The <code>onRequest()</code> function must be overriden. This function is called whenever Pistache received data and correctly parsed it as an HTTP request.</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token keyword"style="font-style:italic">virtual</span><spanclass="token plain"></span><spanclass="token keyword"style="font-style:italic">void</span><spanclass="token plain"></span><spanclass="token function"style="color:rgb(130, 170, 255)">onRequest</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token keyword"style="font-style:italic">const</span><spanclass="token plain"> Http</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Request</span><spanclass="token operator"style="color:rgb(137, 221, 255)">&</span><spanclass="token plain"> request</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"> Http</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">ResponseWriter response</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>The first argument is an object of type <code>Http::Request</code> representing the request itself. It contains a bunch of informations including:</p><ul><li>The resource associated to the request</li><li>The query parameters</li><li>The headers</li><li>The body of the request</li></ul><p>The <code>Request</code> object gives a read-only access to these informations. You can access them through a couple of getters but can not modify them. An HTTP request is <strong>immutable</strong>.</p><h2><aaria-hidden="true"tabindex="-1"class="anchor enhancedAnchor_2LWZ"id="sending-a-response"></a>Sending a response<aclass="hash-link"href="#sending-a-response"title="Direct link to heading">#</a></h2><p><code>ResponseWriter</code> is an object from which the final HTTP response is sent to the client. The <code>onRequest()</code> function does not return anything (<code>void</code>). Instead, the response is sent through the <code>ResponseWriter</code> class. This class provides a bunch of <code>send()</code> function overloads to send the response:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">Async</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Promise</span><spanclass="token operator"style="color:rgb(137, 221, 255)"><</span><spanclass="token plain">ssize_t</span><spanclass="token operator"style="color:rgb(137, 221, 255)">></span><spanclass="token plain"></span><spanclass="token function"style="color:rgb(130, 170, 255)">send</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain">Code code</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>You can use this overload to send a response with an empty body and a given HTTP Code (e.g <code>Http::Code::Ok</code>)</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">Async</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Promise</span><spanclass="token operator"style="color:rgb(137, 221, 255)"><</span><spanclass="token plain">ssize_t</span><spanclass="token operator"style="color:rgb(137, 221, 255)">></span><spanclass="token plain"></span><spanclass="token function"style="color:rgb(130, 170, 255)">send</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> Code code</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token keyword"style="font-style:italic">const</span><spanclass="token plain"> std</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">string</span><spanclass="token operator"style="color:rgb(137, 221, 255)">&</span><spanclass="token plain"> body</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token keyword"style="font-style:italic">const</span><spanclass="token plain"> Mime</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">MediaType </span><spanclass="token operator"style="color:rgb(137, 221, 255)">&</span><spanclass="token plain">mime </span><spanclass="token operator"style="color:rgb(137, 221, 255)">=</span><spanclass="token plain"></span><spanclass="token class-name"style="color:rgb(255, 203, 107)">Mime</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token function"style="color:rgb(130, 170, 255)">MediaType</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>This overload can be used to send a response with static, fixed-size content (body). A MIME type can also be specified, which will be sent through the <code>Content-Type</code> header.</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token keyword"style="font-style:italic">template</span><spanclass="token operator"style="color:rgb(137, 221, 255)"><</span><spanclass="token plain">size_t N</span><spanclass="token operator"style="color:rgb(137, 221, 255)">></span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">Async</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Promise</span><spanclass="token operator"style="color:rgb(137, 221, 255)"><</span><spanclass="token plain">ssize_t</span><spanclass="token operator"style="color:rgb(137, 221, 255)">></span><spanclass="token plain"></span><spanclass="token function"style="color:rgb(130, 170, 255)">send</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> Code code</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token keyword"style="font-style:italic">const</span><spanclass="token plain"></span><spanclass="token keyword"style="font-style:italic">char</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token operator"style="color:rgb(137, 221, 255)">&</span><spanclass="token plain">arr</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">[</span><spanclass="token plain">N</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">]</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token keyword"style="font-style:italic">const</span><spanclass="token plain"> Mime</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">MediaType</span><spanclass="token operator"style="color:rgb(137, 221, 255)">&</span><spanclass="token plain"> mime </span><spanclass="token operator"style="color:rgb(137, 221, 255)">=</span><spanclass="token plain"></span><spanclass="token class-name"style="color:rgb(255, 203, 107)">Mime</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token function"style="color:rgb(130, 170, 255)">MediaType</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>This version can also be used to send a fixed-size response with a body except that it does not need to construct a string (no memory is allocated). The size of the content is directly deduced by the compiler. This version only works with raw string literals.</p><p>These functions are asynchronous, meaning that they do not return a plain old <code>ssize_t</code> value indicating the number of bytes being sent, but instead a <code>Promise</code> that will be fulfilled later on. See the next section for more details on asynchronous programming with Pistache.</p><h2><aaria-hidden="true"tabindex="-1"class="anchor enhancedAnchor_2LWZ"id="response-streaming"></a>Response streaming<aclass="hash-link"href="#response-streaming"title="Direct link to heading">#</a></h2><p>Sometimes, content that is to be sent back to the user can not be known in advance, thus the length can not be determined in advance. For that matter, the HTTP specification defines a special data-transfer mechanism called <ahref="https://tools.ietf.org/html/rfc7230#section-4.1"target="_blank"rel="noopener noreferrer">chunked encoding</a> where data is sent in a series of <em>chunks</em>. This mechanism uses the <code>Transfer-Encoding</code> HTTP header in place of the <code>Content-Length</code> one.</p><p>To stream content, Pistache provides a special <code>ResponseStream</code> class. To get a <code>ResponseStream</code> from a <code>ResponseWriter</code>, call the <code>stream()</code> member function:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token keyword"style="font-style:italic">auto</span><spanclass="token plain"> stream </span><spanclass="token operator"style="color:rgb(137, 221, 255)">=</span><spanclass="token plain"> response</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token function"style="color:rgb(130, 170, 255)">stream</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain">Http</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Code</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Ok</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>To initate a stream, you have to pass the HTTP status code to the stream function (here <code>Http::Code::Ok</code> or <code>HTTP 200</code>). The <code>ResponseStream</code> class provides an <code>iostream</code> like interface that overloads the <code><<</code> operator.</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">stream </span><spanclass="token operator"style="color:rgb(137, 221, 255)"><<</span><spanclass="token plain"></span><spanclass="token string"style="color:rgb(195, 232, 141)">"PO"</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">stream </span><spanclass="token operator"style="color:rgb(137, 221, 255)"><<</span><spanclass="token plain"></span><spanclass="token string"style="color:rgb(195, 232, 141)">"NG"</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>The first line will write a chunk of size 2 with the content <em>PO</em> to the stream's buffer. The second line will write a second chunk of size 2 with the content <em>NG</em>. To end the stream and flush the content, use the special <code>ends</code> marker:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">stream </span><spanclass="token operator"style="color:rgb(137, 221, 255)"><<</span><spanclass="token plain"> ends</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>The <code>ends</code> marker will write the last chunk of size 0 and send the final data over the network. To simply flush the stream's buffer without ending the stream, you can use the <code>flush</code> marker:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">stream </span><spanclass="token operator"style="color:rgb(137, 221, 255)"><<</span><spanclass="token plain"> flush</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><divclass="admonition admonition-caution alert alert--warning"><divclass="admonition-heading"><h5><spanclass="admonition-icon"><svgxmlns="http://www.w3.org/2000/svg"width="16"height="16"viewBox="0 0 16 16"><pathfill-rule="evenodd"d="M8.893 1.5c-.183-.31-.52-.5-.887-.5s-.703.19-.886.5L.138 13.499a.98.98 0 0 0 0 1.001c.193.31.53.501.886.501h13.964c.367 0 .704-.19.877-.5a1.03 1.03 0 0 0 .01-1.002L8.893 1.5zm.133 11.497H6.987v-2.003h2.039v2.003zm0-3.004H6.987V5.987h2.039v4.006z"></path></svg></span>Headers writing</h5></div><divclass="admonition-content"><p>After starting a stream, headers become immutable. They must be written to the response before creating a <code>ResponseStream</code>:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">response</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token function"style="color:rgb(130, 170, 255)">headers</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token plain">add</span><spanclass="token operator"style="color:rgb(137, 221, 255)"><</span><spanclass="token plain">Header</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Server</span><spanclass="token operator"style="color:rgb(137, 221, 255)">></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token string"style="color:rgb(195, 232, 141)">"lys"</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token plain">add</span><spanclass="token operator"style="color:rgb(137, 221, 255)"><</span><spanclass="token plain">Header</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">ContentType</span><spanclass="token operator"style="color:rgb(137, 221, 255)">></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token function"style="color:rgb(130, 170, 255)">MIME</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain">Text</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"> Plain</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"style="display:inline-block">
</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token keyword"style="font-style:italic">auto</span><spanclass="token plain"> stream </span><spanclass="token operator"style="color:rgb(137, 221, 255)">=</span><spanclass="token plain"> response</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token function"style="color:rgb(130, 170, 255)">stream</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">stream </span><spanclass="token operator"style="color:rgb(137, 221, 255)"><<</span><spanclass="token plain"></span><spanclass="token string"style="color:rgb(195, 232, 141)">"PO"</span><spanclass="token plain"></span><spanclass="token operator"style="color:rgb(137, 221, 255)"><<</span><spanclass="token plain"></span><spanclass="token string"style="color:rgb(195, 232, 141)">"NG"</span><spanclass="token plain"></span><spanclass="token operator"style="color:rgb(137, 221, 255)"><<</span><spanclass="token plain"> ends</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div></div></div><h2><aaria-hidden="true"tabindex="-1"class="anchor enhancedAnchor_2LWZ"id="static-file-serving"></a>Static file serving<aclass="hash-link"href="#static-file-serving"title="Direct link to heading">#</a></h2><p>In addition to text content serving, Pistache provides a way to serve static files through the <code>Http::serveFile</code> function:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token keyword"style="font-style:italic">if</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain">request</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token function"style="color:rgb(130, 170, 255)">resource</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token plain"></span><spanclass="token operator"style="color:rgb(137, 221, 255)">==</span><spanclass="token plain"></span><spanclass="token string"style="color:rgb(195, 232, 141)">"/doc"</span><spanclass="token plain"></span><spanclass="token operator"style="color:rgb(137, 221, 255)">&&</span><spanclass="token plain"> request</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token plain">method </span><spanclass="token operator"style="color:rgb(137, 221, 255)">==</span><spanclass="token plain"> Http</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Method</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Get</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">{</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token class-name"style="color:rgb(255, 203, 107)">Http</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token function"style="color:rgb(130, 170, 255)">serveFile</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain">response</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span><spanclass="token string"style="color:rgb(195, 232, 141)">"README.md"</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">}</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><divclass="admonition admonition-note alert alert--secondary"><divclass="admonition-heading"><h5><spanclass="admonition-icon"><svgxmlns="http://www.w3.org/2000/svg"width="14"height="16"viewBox="0 0 14 16"><pathfill-rule="evenodd"d="M6.3 5.69a.942.942 0 0 1-.28-.7c0-.28.09-.52.28-.7.19-.18.42-.28.7-.28.28 0 .52.09.7.28.18.19.28.42.28.7 0 .28-.09.52-.28.7a1 1 0 0 1-.7.3c-.28 0-.52-.11-.7-.3zM8 7.99c-.02-.25-.11-.48-.31-.69-.2-.19-.42-.3-.69-.31H6c-.27.02-.48.13-.69.31-.2.2-.3.44-.31.69h1v3c.02.27.11.5.31.69.2.2.42.31.69.31h1c.27 0 .48-.11.69-.31.2-.19.3-.42.31-.69H8V7.98v.01zM7 2.3c-3.14 0-5.7 2.54-5.7 5.68 0 3.14 2.56 5.7 5.7 5.7s5.7-2.55 5.7-5.7c0-3.15-2.56-5.69-5.7-5.69v.01zM7 .98c3.86 0 7 3.14 7 7s-3.14 7-7 7-7-3.12-7-7 3.14-7 7-7z"></path></svg></span>Return value</h5></div><divclass="admonition-content"><p><code>serveFile</code> also returns a <code>Promise</code> representing the total number of bytes being sent to the wire</p></div></div><h2><aaria-hidden="true"tabindex="-1"class="anchor enhancedAnchor_2LWZ"id="controlling-timeout"></a>Controlling timeout<aclass="hash-link"href="#controlling-timeout"title="Direct link to heading">#</a></h2><p>Sometimes, you might require to timeout after a certain amount of time. For example, if you are designing an HTTP API with soft real-time constraints, you will have a time constraint to send a response back to the client. That is why Pistache provides the ability to control the timeout on a per-request basis. To arm a timeout on a response, you can use the <code>timeoutAfter()</code> member function directly on the <code>ResponseWriter</code> object:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">response</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token function"style="color:rgb(130, 170, 255)">timeoutAfter</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain">std</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">chrono</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token function"style="color:rgb(130, 170, 255)">milliseconds</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token number"style="color:rgb(247, 140, 108)">500</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>This will trigger a timeout if a response has not been sent within 500 milliseconds. <code>timeoutAfter</code> accepts any kind of duration.</p><p>When a timeout triggers, the <code>onTimeout()</code> function from your handler will be called. By default, this method does nothing. If you want to handle your timeout properly, you should then override this function inside your own handler:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token keyword"style="font-style:italic">void</span><spanclass="token plain"></span><spanclass="token function"style="color:rgb(130, 170, 255)">onTimeout</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token keyword"style="font-style:italic">const</span><spanclass="token plain"> Http</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Request</span><spanclass="token operator"style="color:rgb(137, 221, 255)">&</span><spanclass="token plain"> request</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"> Http</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">ResponseWriter writer</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">{</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> request</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token function"style="color:rgb(130, 170, 255)">send</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain">Http</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Code</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">No_Content</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">}</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>The <code>Request</code> object that is passed to the <code>onTimeout</code> is the exact same request that triggered the timeout. The <code>ResponseWriter</code> is a complete new writer object.</p><divclass="admonition admonition-note alert alert--secondary"><divclass="admonition-heading"><h5><spanclass="admonition-icon"><svgxmlns="http://www.w3.org/2000/svg"width="14"height="16"viewBox="0 0 14 16"><pathfill-rule="evenodd"d="M6.3 5.69a.942.942 0 0 1-.28-.7c0-.28.09-.52.28-.7.19-.18.42-.28.7-.28.28 0 .52.09.7.28.18.19.28.42.28.7 0 .28-.09.52-.28.7a1 1 0 0 1-.7.3c-.28 0-.52-.11-.7-.3zM8 7.99c-.02-.25-.11-.48-.31-.69-.2-.19-.42-.3-.69-.31H6c-.27.02-.48.13-.69.31-.2.2-.3.44-.31.69h1v3c.02.27.11.5.31.69.2.2.42.31.69.31h1c.27 0 .48-.11.69-.31.2-.19.3-.42.31-.69H8V7.98v.01zM7 2.3c-3.14 0-5.7 2.54-5.7 5.68 0 3.14 2.56 5.7 5.7 5.7s5.7-2.55 5.7-5.7c0-3.15-2.56-5.69-5.7-5.69v.01zM7 .98c3.86 0 7 3.14 7 7s-3.14 7-7 7-7-3.12-7-7 3.14-7 7-7z"></path></svg></span>ResponseWriter state</h5></div><divclass="admonition-content"><p>Since the <code>ResponseWriter</code> object is a complete new object, state is not preserved with the <code>ResponseWriter</code> from the <code>onRequest()</code> callback, which means that you will have to write the complete response again, including headers and cookies.</p></div></div></div><footerclass="row docusaurus-mt-lg"><divclass="col"><ahref="https://github.com/pistacheio/pistache/edit/master/pistache.io/docs/http-handler.md"target="_blank"rel="noreferrer noopener"><svgfill="currentColor"height="20"width="20"viewBox="0 0 40 40"class="iconEdit_2_ui"aria-hidden="true"><g><pathd="m34.5 11.7l-3 3.1-6.3-6.3 3.1-3q0.5-0.5 1.2-0.5t1.1 0.5l3.9 3.9q0.5 0.4 0.5 1.1t-0.5 1.2z m-29.5 17.1l18.4-18.5 6.3 6.3-18.4 18.4h-6.3v-6.2z"></path></g></svg>Edit this page</a></div><divclass="col lastUpdated_3DPF"></div></footer></article><navclass="pagination-nav docusaurus-mt-lg"aria-label="Docs pages navigation"><divclass="pagination-nav__item"><aclass="pagination-nav__link"href="/docs/headers"><divclass="pagination-nav__sublabel">Previous</div><divclass="pagination-nav__label">« Headers</div></a></div><divclass="pagination-nav__item pagination-nav__item--next"><aclass="pagination-nav__link"href="/docs/"><divclass="pagination-nav__sublabel">Next</div><divclass="pagination-nav__label">Getting started »</div></a></div></nav></div></div><divclass="col col--3"><divclass="tableOfContents_35-E thin-scrollbar"><ulclass="table-of-contents table-of-contents__left-border"><li><ahref="#sending-a-response"class="table-of-contents__link">Sending a response</a></li><li><ahref="#response-streaming"class="table-of-contents__link">Response streaming</a></li><li><ahref="#static-file-serving"class="table-of-contents__link">Static file serving</a></li><li><ahref="#controlling-timeout"class="table-of-contents__link">Controlling timeout</a></li></ul></div></div></div></div></main></div></div><footerclass="footer footer--dark"><divclass="container"><divclass="row footer__links"><divclass="col footer__col"><divclass="footer__title">Docs</div><ulclass="footer__items"><liclass="footer__item"><aclass="footer__link-item"href="/docs/">Quickstart</a></li><liclass="footer__item"><aclass="footer__link-item"href="/docs/http-handler/">User guide</a></li></ul></div><divclass="col footer__col"><divclass="footer__title">Community</div><ulclass="footer__items"><liclass="footer__item"><ahref="https://stackoverflow.com/questions/tagged/pistache"target="_blank"rel="noopener noreferrer"class="footer__link-item">Stack Overflow</a></li><liclass="footer__item"><ahref="https://web.libera.chat/#pistache"target="_blank"rel="noopener noreferrer"class="footer__link-item">#pistache on Libera.Chat</a></li></ul></div><divclass="col footer__col"><divclass="footer__title">More</div><ulclass="footer__items"><liclass="footer__item"><ahref="https://github.com/pistacheio/pistache"target="_blank"rel="noopener noreferrer"class="footer__link-item">GitHub</a></li></ul></div></div><divclass="footer__bottom text--center"><divclass="footer__copyright">Pistache, 2015 - 2021</div></div></div></footer></div>
</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token keyword"style="font-style:italic">auto</span><spanclass="token plain"> stream </span><spanclass="token operator"style="color:rgb(137, 221, 255)">=</span><spanclass="token plain"> response</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token function"style="color:rgb(130, 170, 255)">stream</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">stream </span><spanclass="token operator"style="color:rgb(137, 221, 255)"><<</span><spanclass="token plain"></span><spanclass="token string"style="color:rgb(195, 232, 141)">"PO"</span><spanclass="token plain"></span><spanclass="token operator"style="color:rgb(137, 221, 255)"><<</span><spanclass="token plain"></span><spanclass="token string"style="color:rgb(195, 232, 141)">"NG"</span><spanclass="token plain"></span><spanclass="token operator"style="color:rgb(137, 221, 255)"><<</span><spanclass="token plain"> ends</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div></div></div><h2><aaria-hidden="true"tabindex="-1"class="anchor enhancedAnchor_2LWZ"id="static-file-serving"></a>Static file serving<aclass="hash-link"href="#static-file-serving"title="Direct link to heading">#</a></h2><p>In addition to text content serving, Pistache provides a way to serve static files through the <code>Http::serveFile</code> function:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token keyword"style="font-style:italic">if</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain">request</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token function"style="color:rgb(130, 170, 255)">resource</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token plain"></span><spanclass="token operator"style="color:rgb(137, 221, 255)">==</span><spanclass="token plain"></span><spanclass="token string"style="color:rgb(195, 232, 141)">"/doc"</span><spanclass="token plain"></span><spanclass="token operator"style="color:rgb(137, 221, 255)">&&</span><spanclass="token plain"> request</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token plain">method </span><spanclass="token operator"style="color:rgb(137, 221, 255)">==</span><spanclass="token plain"> Http</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Method</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Get</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">{</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token class-name"style="color:rgb(255, 203, 107)">Http</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token function"style="color:rgb(130, 170, 255)">serveFile</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain">response</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span><spanclass="token string"style="color:rgb(195, 232, 141)">"README.md"</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">}</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><divclass="admonition admonition-note alert alert--secondary"><divclass="admonition-heading"><h5><spanclass="admonition-icon"><svgxmlns="http://www.w3.org/2000/svg"width="14"height="16"viewBox="0 0 14 16"><pathfill-rule="evenodd"d="M6.3 5.69a.942.942 0 0 1-.28-.7c0-.28.09-.52.28-.7.19-.18.42-.28.7-.28.28 0 .52.09.7.28.18.19.28.42.28.7 0 .28-.09.52-.28.7a1 1 0 0 1-.7.3c-.28 0-.52-.11-.7-.3zM8 7.99c-.02-.25-.11-.48-.31-.69-.2-.19-.42-.3-.69-.31H6c-.27.02-.48.13-.69.31-.2.2-.3.44-.31.69h1v3c.02.27.11.5.31.69.2.2.42.31.69.31h1c.27 0 .48-.11.69-.31.2-.19.3-.42.31-.69H8V7.98v.01zM7 2.3c-3.14 0-5.7 2.54-5.7 5.68 0 3.14 2.56 5.7 5.7 5.7s5.7-2.55 5.7-5.7c0-3.15-2.56-5.69-5.7-5.69v.01zM7 .98c3.86 0 7 3.14 7 7s-3.14 7-7 7-7-3.12-7-7 3.14-7 7-7z"></path></svg></span>Return value</h5></div><divclass="admonition-content"><p><code>serveFile</code> also returns a <code>Promise</code> representing the total number of bytes being sent to the wire</p></div></div><h2><aaria-hidden="true"tabindex="-1"class="anchor enhancedAnchor_2LWZ"id="controlling-timeout"></a>Controlling timeout<aclass="hash-link"href="#controlling-timeout"title="Direct link to heading">#</a></h2><p>Sometimes, you might require to timeout after a certain amount of time. For example, if you are designing an HTTP API with soft real-time constraints, you will have a time constraint to send a response back to the client. That is why Pistache provides the ability to control the timeout on a per-request basis. To arm a timeout on a response, you can use the <code>timeoutAfter()</code> member function directly on the <code>ResponseWriter</code> object:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">response</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token function"style="color:rgb(130, 170, 255)">timeoutAfter</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain">std</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">chrono</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token function"style="color:rgb(130, 170, 255)">milliseconds</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token number"style="color:rgb(247, 140, 108)">500</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>This will trigger a timeout if a response has not been sent within 500 milliseconds. <code>timeoutAfter</code> accepts any kind of duration.</p><p>When a timeout triggers, the <code>onTimeout()</code> function from your handler will be called. By default, this method does nothing. If you want to handle your timeout properly, you should then override this function inside your own handler:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token keyword"style="font-style:italic">void</span><spanclass="token plain"></span><spanclass="token function"style="color:rgb(130, 170, 255)">onTimeout</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token keyword"style="font-style:italic">const</span><spanclass="token plain"> Http</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Request</span><spanclass="token operator"style="color:rgb(137, 221, 255)">&</span><spanclass="token plain"> request</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"> Http</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">ResponseWriter writer</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">{</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"> request</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token function"style="color:rgb(130, 170, 255)">send</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain">Http</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Code</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">No_Content</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">}</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>The <code>Request</code> object that is passed to the <code>onTimeout</code> is the exact same request that triggered the timeout. The <code>ResponseWriter</code> is a complete new writer object.</p><divclass="admonition admonition-note alert alert--secondary"><divclass="admonition-heading"><h5><spanclass="admonition-icon"><svgxmlns="http://www.w3.org/2000/svg"width="14"height="16"viewBox="0 0 14 16"><pathfill-rule="evenodd"d="M6.3 5.69a.942.942 0 0 1-.28-.7c0-.28.09-.52.28-.7.19-.18.42-.28.7-.28.28 0 .52.09.7.28.18.19.28.42.28.7 0 .28-.09.52-.28.7a1 1 0 0 1-.7.3c-.28 0-.52-.11-.7-.3zM8 7.99c-.02-.25-.11-.48-.31-.69-.2-.19-.42-.3-.69-.31H6c-.27.02-.48.13-.69.31-.2.2-.3.44-.31.69h1v3c.02.27.11.5.31.69.2.2.42.31.69.31h1c.27 0 .48-.11.69-.31.2-.19.3-.42.31-.69H8V7.98v.01zM7 2.3c-3.14 0-5.7 2.54-5.7 5.68 0 3.14 2.56 5.7 5.7 5.7s5.7-2.55 5.7-5.7c0-3.15-2.56-5.69-5.7-5.69v.01zM7 .98c3.86 0 7 3.14 7 7s-3.14 7-7 7-7-3.12-7-7 3.14-7 7-7z"></path></svg></span>ResponseWriter state</h5></div><divclass="admonition-content"><p>Since the <code>ResponseWriter</code> object is a complete new object, state is not preserved with the <code>ResponseWriter</code> from the <code>onRequest()</code> callback, which means that you will have to write the complete response again, including headers and cookies.</p></div></div></div><footerclass="row docusaurus-mt-lg"><divclass="col"><ahref="https://github.com/pistacheio/pistache/edit/master/pistache.io/docs/http-handler.md"target="_blank"rel="noreferrer noopener"><svgfill="currentColor"height="20"width="20"viewBox="0 0 40 40"class="iconEdit_2_ui"aria-hidden="true"><g><pathd="m34.5 11.7l-3 3.1-6.3-6.3 3.1-3q0.5-0.5 1.2-0.5t1.1 0.5l3.9 3.9q0.5 0.4 0.5 1.1t-0.5 1.2z m-29.5 17.1l18.4-18.5 6.3 6.3-18.4 18.4h-6.3v-6.2z"></path></g></svg>Edit this page</a></div><divclass="col lastUpdated_3DPF"></div></footer></article><navclass="pagination-nav docusaurus-mt-lg"aria-label="Docs pages navigation"><divclass="pagination-nav__item"><aclass="pagination-nav__link"href="/docs/headers"><divclass="pagination-nav__sublabel">Previous</div><divclass="pagination-nav__label">« Headers</div></a></div><divclass="pagination-nav__item pagination-nav__item--next"><aclass="pagination-nav__link"href="/docs/"><divclass="pagination-nav__sublabel">Next</div><divclass="pagination-nav__label">Getting started »</div></a></div></nav></div></div><divclass="col col--3"><divclass="tableOfContents_35-E thin-scrollbar"><ulclass="table-of-contents table-of-contents__left-border"><li><ahref="#sending-a-response"class="table-of-contents__link">Sending a response</a></li><li><ahref="#response-streaming"class="table-of-contents__link">Response streaming</a></li><li><ahref="#static-file-serving"class="table-of-contents__link">Static file serving</a></li><li><ahref="#controlling-timeout"class="table-of-contents__link">Controlling timeout</a></li></ul></div></div></div></div></main></div></div><footerclass="footer footer--dark"><divclass="container"><divclass="row footer__links"><divclass="col footer__col"><divclass="footer__title">Docs</div><ulclass="footer__items"><liclass="footer__item"><aclass="footer__link-item"href="/docs/">Quickstart</a></li><liclass="footer__item"><aclass="footer__link-item"href="/docs/http-handler/">User guide</a></li></ul></div><divclass="col footer__col"><divclass="footer__title">Community</div><ulclass="footer__items"><liclass="footer__item"><ahref="https://stackoverflow.com/questions/tagged/pistache"target="_blank"rel="noopener noreferrer"class="footer__link-item">Stack Overflow</a></li><liclass="footer__item"><ahref="https://web.libera.chat/#pistache"target="_blank"rel="noopener noreferrer"class="footer__link-item">#pistache on Libera.Chat</a></li></ul></div><divclass="col footer__col"><divclass="footer__title">More</div><ulclass="footer__items"><liclass="footer__item"><ahref="https://github.com/pistacheio/pistache"target="_blank"rel="noopener noreferrer"class="footer__link-item">GitHub</a></li></ul></div></div><divclass="footer__bottom text--center"><divclass="footer__copyright">Pistache, 2015 - 2021</div></div></div></footer></div>
<titledata-react-helmet="true">Getting started | Pistache</title><metadata-react-helmet="true"property="og:url"content="https://pistache.io/docs/"><metadata-react-helmet="true"name="docusaurus_locale"content="en"><metadata-react-helmet="true"name="docusaurus_version"content="current"><metadata-react-helmet="true"name="docusaurus_tag"content="docs-default-current"><metadata-react-helmet="true"property="og:title"content="Getting started | Pistache"><metadata-react-helmet="true"name="description"content="<!--"><metadata-react-helmet="true"property="og:description"content="<!--"><linkdata-react-helmet="true"rel="shortcut icon"href="/img/logo.png"><linkdata-react-helmet="true"rel="canonical"href="https://pistache.io/docs/"><linkdata-react-helmet="true"rel="alternate"href="https://pistache.io/docs/"hreflang="en"><linkdata-react-helmet="true"rel="alternate"href="https://pistache.io/docs/"hreflang="x-default"><linkrel="stylesheet"href="/assets/css/styles.35f9fda9.css">
<titledata-react-helmet="true">Getting started | Pistache</title><metadata-react-helmet="true"property="og:url"content="https://pistache.io/docs/"><metadata-react-helmet="true"name="docusaurus_locale"content="en"><metadata-react-helmet="true"name="docusaurus_version"content="current"><metadata-react-helmet="true"name="docusaurus_tag"content="docs-default-current"><metadata-react-helmet="true"property="og:title"content="Getting started | Pistache"><metadata-react-helmet="true"name="description"content="<!--"><metadata-react-helmet="true"property="og:description"content="<!--"><linkdata-react-helmet="true"rel="shortcut icon"href="/img/logo.png"><linkdata-react-helmet="true"rel="canonical"href="https://pistache.io/docs/"><linkdata-react-helmet="true"rel="alternate"href="https://pistache.io/docs/"hreflang="en"><linkdata-react-helmet="true"rel="alternate"href="https://pistache.io/docs/"hreflang="x-default"><linkrel="stylesheet"href="/assets/css/styles.35f9fda9.css">
<div><ahref="#main"class="skipToContent_1oUP shadow--md">Skip to main content</a></div><navclass="navbar navbar--fixed-top"><divclass="navbar__inner"><divclass="navbar__items"><buttonaria-label="Navigation bar toggle"class="navbar__toggle clean-btn"type="button"tabindex="0"><svgwidth="30"height="30"viewBox="0 0 30 30"aria-hidden="true"><pathstroke="currentColor"stroke-linecap="round"stroke-miterlimit="10"stroke-width="2"d="M4 7h22M4 15h22M4 23h22"></path></svg></button><aclass="navbar__brand"href="/"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--light_3UqQ navbar__logo"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--dark_hz6m navbar__logo"><bclass="navbar__title">Pistache</b></a><aaria-current="page"class="navbar__item navbar__link navbar__link--active"href="/docs/">Docs</a></div><divclass="navbar__items navbar__items--right"><ahref="https://github.com/pistacheio/pistache"target="_blank"rel="noopener noreferrer"class="navbar__item navbar__link header-github-link"aria-label="GitHub repository"></a><divclass="react-toggle displayOnlyInLargeViewport_GrZ2 react-toggle--disabled"><divclass="react-toggle-track"role="button"tabindex="-1"><divclass="react-toggle-track-check"><spanclass="toggle_71bT">🌜</span></div><divclass="react-toggle-track-x"><spanclass="toggle_71bT">🌞</span></div><divclass="react-toggle-thumb"></div></div><inputtype="checkbox"class="react-toggle-screenreader-only"aria-label="Switch between dark and light mode"></div></div></div><divrole="presentation"class="navbar-sidebar__backdrop"></div><divclass="navbar-sidebar"><divclass="navbar-sidebar__brand"><aclass="navbar__brand"href="/"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--light_3UqQ navbar__logo"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--dark_hz6m navbar__logo"><bclass="navbar__title">Pistache</b></a></div><divclass="navbar-sidebar__items"><divclass="menu"><ulclass="menu__list"><liclass="menu__list-item"><aaria-current="page"class="menu__link navbar__link--active"href="/docs/">Docs</a></li><liclass="menu__list-item"><ahref="https://github.com/pistacheio/pistache"target="_blank"rel="noopener noreferrer"class="menu__link header-github-link"aria-label="GitHub repository"></a></li></ul></div></div></div></nav><divclass="main-wrapper docs-wrapper doc-page"><divclass="docPage_31aa"><asideclass="docSidebarContainer_3Kbt"><divclass="sidebar_15mo"><navclass="menu menu--responsive thin-scrollbar menu_Bmed"aria-label="Sidebar navigation"><buttonaria-label="Open menu"aria-haspopup="true"class="button button--secondary button--sm menu__button"type="button"><svgclass="sidebarMenuIcon_fgN0"width="24"height="24"viewBox="0 0 30 30"aria-hidden="true"><pathstroke="currentColor"stroke-linecap="round"stroke-miterlimit="10"stroke-width="2"d="M4 7h22M4 15h22M4 23h22"></path></svg></button><ulclass="menu__list"><liclass="menu__list-item"><aclass="menu__link"href="/docs/asynchronous-http-programming">Asynchronous HTTP programming</a></li><liclass="menu__list-item"><aclass="menu__link"href="/docs/headers">Headers</a></li><liclass="menu__list-item"><aclass="menu__link"href="/docs/http-handler">HTTP handler</a></li><liclass="menu__list-item"><aclass="menu__link"href="/docs/">Getting started</a></li><liclass="menu__list-item"><aclass="menu__link"href="/docs/rest-description">REST description</a></li><liclass="menu__list-item"><aaria-current="page"class="menu__link menu__link--active active"href="/docs/routing">Routing</a></li></ul></nav></div></aside><mainclass="docMainContainer_3ufF"><divclass="container padding-top--md padding-bottom--lg docItemWrapper_3FMP"><divclass="row"><divclass="col docItemCol_3FnS"><divclass="docItemContainer_33ec"><article><divclass="markdown"><header><h1class="h1Heading_27L5">Routing</h1></header><p>HTTP routing consists of binding an HTTP route to a C++ callback. A special component called an HTTP router will be in charge of dispatching HTTP requests to the right C++ callback. A route is composed of an HTTP verb associated to a resource:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly javascript"><pretabindex="0"class="prism-code language-javascript codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token constant"style="color:rgb(130, 170, 255)">GET</span><spanclass="token plain"></span><spanclass="token operator"style="color:rgb(137, 221, 255)">/</span><spanclass="token plain">users</span><spanclass="token operator"style="color:rgb(137, 221, 255)">/</span><spanclass="token number"style="color:rgb(247, 140, 108)">1</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>Here, <code>GET</code> is the verb and <code>/users/1</code> is the associated resource.</p><h2><aaria-hidden="true"tabindex="-1"class="anchor enhancedAnchor_2LWZ"id="http-methods"></a>HTTP methods<aclass="hash-link"href="#http-methods"title="Direct link to heading">#</a></h2><p>A bunch of HTTP methods (verbs) are supported by Pistache:</p><ul><li><em>GET</em>: The <code>GET</code> method is used by the client (e.g browser) to retrieve a resource identified by an URI. For example, to retrieve an user identified by an id, a client will issue a <code>GET</code> to the <code>/users/:id</code> Request-URI.</li><li><em>POST</em>: the <code>POST</code> method is used to post or send new information to a certain resource. The server will then read and store the data associated to the request. <code>POST</code> is a common way of transmitting data from an HTML form. <code>POST</code> can also be used to create a new resource or update information of an existing resource. For example, to create a new user, a client will issue a <code>POST</code> to the <code>/users</code> path with the data of the user to create in its body.</li><li><em>PUT</em>: <code>PUT</code> is very similar to <code>POST</code> except that <code>PUT</code> is idempotent, meaning that two requests to the same Request-URI with the same identical content should have the same effect and should produce the same result.</li><li><em>DELETE</em>: the <code>DELETE</code> method is used to delete a resource associated to a given Request-URI. For example, to remove an user, a client might issue a <code>DELETE</code> call to the <code>/users/:id</code> Request-URI.</li></ul><p>To sum up, <code>POST</code> and <code>PUT</code> are used to Create and/or Update, <code>GET</code> is used to Read and <code>DELETE</code> is used to Delete information.</p><h2><aaria-hidden="true"tabindex="-1"class="anchor enhancedAnchor_2LWZ"id="route-patterns"></a>Route patterns<aclass="hash-link"href="#route-patterns"title="Direct link to heading">#</a></h2><h3><aaria-hidden="true"tabindex="-1"class="anchor enhancedAnchor_2LWZ"id="static-routes"></a>Static routes<aclass="hash-link"href="#static-routes"title="Direct link to heading">#</a></h3><p>Static routes are the simplest ones as they do rely on dynamic parts of the Request-URI. For example <code>/users/all</code> is a static route that will exactly match the <code>/users/all</code> Request-URI.</p><h3><aaria-hidden="true"tabindex="-1"class="anchor enhancedAnchor_2LWZ"id="dynamic-routes"></a>Dynamic routes<aclass="hash-link"href="#dynamic-routes"title="Direct link to heading">#</a></h3><p>However, it is often useful to define routes that have dynamic parts. For example, to retrieve a specific user by its id, the id is needed to query the storage. Dynamic routes thus have parameters that are then matched one by one by the HTTP router. In a dynamic route, parameters are identified by a column <code>:</code></p><p><code>/users/:id</code></p><p>Here, <code>:id</code> is a dynamic parameter. When a request comes in, the router will try to match the <code>:id</code> parameter to the corresponding part of the request. For example, if the server receives a request to <code>/users/13</code>, the router will match the <code>13</code> value to the <code>:id</code> parameter.</p><p>Some parameters, like <code>:id</code> are named. However, Pistache also allows <em>splat</em> (wildcard) parameters, identified by a star <code>*</code>:</p><p><code>/link/*/to/*</code></p><h2><aaria-hidden="true"tabindex="-1"class="anchor enhancedAnchor_2LWZ"id="defining-routes"></a>Defining routes<aclass="hash-link"href="#defining-routes"title="Direct link to heading">#</a></h2><p>To define your routes, you first have to instantiate an HTTP router:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">Http</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Router router</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>Then, use the <code>Routes::<Method>()</code> functions to add some routes:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token class-name"style="color:rgb(255, 203, 107)">Routes</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token function"style="color:rgb(130, 170, 255)">Get</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain">router</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span><spanclass="token string"style="color:rgb(195, 232, 141)">"/users/all"</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span><spanclass="token class-name"style="color:rgb(255, 203, 107)">Routes</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token function"style="color:rgb(130, 170, 255)">bind</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token operator"style="color:rgb(137, 221, 255)">&</span><spanclass="token plain">UsersApi</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">getAllUsers</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span><spanclass="token keyword"style="font-style:italic">this</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token class-name"style="color:rgb(255, 203, 107)">Routes</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token function"style="color:rgb(130, 170, 255)">Post</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain">router</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span><spanclass="token string"style="color:rgb(195, 232, 141)">"/users/:id"</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span><spanclass="token class-name"style="color:rgb(255, 203, 107)">Routes</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token function"style="color:rgb(130, 170, 255)">bind</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token operator"style="color:rgb(137, 221, 255)">&</span><spanclass="token plain">UsersApi</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">getUserId</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span><spanclass="token keyword"style="font-style:italic">this</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token class-name"style="color:rgb(255, 203, 107)">Routes</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token function"style="color:rgb(130, 170, 255)">Get</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain">router</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span><spanclass="token string"style="color:rgb(195, 232, 141)">"/link/*/to/*"</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span><spanclass="token class-name"style="color:rgb(255, 203, 107)">Routes</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token function"style="color:rgb(130, 170, 255)">bind</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token operator"style="color:rgb(137, 221, 255)">&</span><spanclass="token plain">UsersApi</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">linkUsers</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span><spanclass="token keyword"style="font-style:italic">this</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p><code>Routes::bind</code> is a special function that will generate a corresponding C++ callback that will then be called by the router if a given route matches the Request-URI.</p><h3><aaria-hidden="true"tabindex="-1"class="anchor enhancedAnchor_2LWZ"id="callbacks"></a>Callbacks<aclass="hash-link"href="#callbacks"title="Direct link to heading">#</a></h3><p>A C++ callback associated to a route must have the following signature:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token keyword"style="font-style:italic">void</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token keyword"style="font-style:italic">const</span><spanclass="token plain"> Rest</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Request</span><spanclass="token operator"style="color:rgb(137, 221, 255)">&</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"> Http</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">ResponseWriter</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>A callback can either be a non-static free or member function. For member functions, a pointer to the corresponding instance must be passed to the Routes::bind function so that the router knows on which instance to invoke the member function.</p><p>The first parameter of the callback is <code>Rest::Request</code> and not an <code>Http::Request</code>. A <code>Rest::Request</code> is an <code>Http::Request</code> with additional functions. Named and splat parameters are for example retrieved through this object:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token keyword"style="font-style:italic">void</span><spanclass="token plain"></span><spanclass="token class-name"style="color:rgb(255, 203, 107)">UsersApi</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token function"style="color:rgb(130, 170, 255)">getUserId</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token keyword"style="font-style:italic">const</span><spanclass="token plain"> Rest</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Request</span><spanclass="token operator"style="color:rgb(137, 221, 255)">&</span><spanclass="token plain"> request</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"> Http</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">ResponseWriter response</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">{</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token keyword"style="font-style:italic">auto</span><spanclass="token plain"> id </span><spanclass="token operator"style="color:rgb(137, 221, 255)">=</span><spanclass="token plain"> request</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token function"style="color:rgb(130, 170, 255)">param</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token string"style="color:rgb(195, 232, 141)">":id"</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token plain">as</span><spanclass="token operator"style="color:rgb(137, 221, 255)"><</span><spanclass="token keyword"style="font-style:italic">int</span><spanclass="token operator"style="color:rgb(137, 221, 255)">></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token comment"style="color:rgb(105, 112, 152);font-style:italic">// ...</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">}</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"style="display:inline-block">
<div><ahref="#main"class="skipToContent_1oUP shadow--md">Skip to main content</a></div><navclass="navbar navbar--fixed-top"><divclass="navbar__inner"><divclass="navbar__items"><buttonaria-label="Navigation bar toggle"class="navbar__toggle clean-btn"type="button"tabindex="0"><svgwidth="30"height="30"viewBox="0 0 30 30"aria-hidden="true"><pathstroke="currentColor"stroke-linecap="round"stroke-miterlimit="10"stroke-width="2"d="M4 7h22M4 15h22M4 23h22"></path></svg></button><aclass="navbar__brand"href="/"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--light_3UqQ navbar__logo"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--dark_hz6m navbar__logo"><bclass="navbar__title">Pistache</b></a><aaria-current="page"class="navbar__item navbar__link navbar__link--active"href="/docs/">Docs</a></div><divclass="navbar__items navbar__items--right"><ahref="https://github.com/pistacheio/pistache"target="_blank"rel="noopener noreferrer"class="navbar__item navbar__link header-github-link"aria-label="GitHub repository"></a><divclass="react-toggle displayOnlyInLargeViewport_GrZ2 react-toggle--disabled"><divclass="react-toggle-track"role="button"tabindex="-1"><divclass="react-toggle-track-check"><spanclass="toggle_71bT">🌜</span></div><divclass="react-toggle-track-x"><spanclass="toggle_71bT">🌞</span></div><divclass="react-toggle-thumb"></div></div><inputtype="checkbox"class="react-toggle-screenreader-only"aria-label="Switch between dark and light mode"></div></div></div><divrole="presentation"class="navbar-sidebar__backdrop"></div><divclass="navbar-sidebar"><divclass="navbar-sidebar__brand"><aclass="navbar__brand"href="/"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--light_3UqQ navbar__logo"><imgsrc="/img/logo.png"alt="Pistache logo"class="themedImage_1VuW themedImage--dark_hz6m navbar__logo"><bclass="navbar__title">Pistache</b></a></div><divclass="navbar-sidebar__items"><divclass="menu"><ulclass="menu__list"><liclass="menu__list-item"><aaria-current="page"class="menu__link navbar__link--active"href="/docs/">Docs</a></li><liclass="menu__list-item"><ahref="https://github.com/pistacheio/pistache"target="_blank"rel="noopener noreferrer"class="menu__link header-github-link"aria-label="GitHub repository"></a></li></ul></div></div></div></nav><divclass="main-wrapper docs-wrapper doc-page"><divclass="docPage_31aa"><asideclass="docSidebarContainer_3Kbt"><divclass="sidebar_15mo"><navclass="menu menu--responsive thin-scrollbar menu_Bmed"aria-label="Sidebar navigation"><buttonaria-label="Open menu"aria-haspopup="true"class="button button--secondary button--sm menu__button"type="button"><svgclass="sidebarMenuIcon_fgN0"width="24"height="24"viewBox="0 0 30 30"aria-hidden="true"><pathstroke="currentColor"stroke-linecap="round"stroke-miterlimit="10"stroke-width="2"d="M4 7h22M4 15h22M4 23h22"></path></svg></button><ulclass="menu__list"><liclass="menu__list-item"><aclass="menu__link"href="/docs/asynchronous-http-programming">Asynchronous HTTP programming</a></li><liclass="menu__list-item"><aclass="menu__link"href="/docs/headers">Headers</a></li><liclass="menu__list-item"><aclass="menu__link"href="/docs/http-handler">HTTP handler</a></li><liclass="menu__list-item"><aclass="menu__link"href="/docs/">Getting started</a></li><liclass="menu__list-item"><aclass="menu__link"href="/docs/rest-description">REST description</a></li><liclass="menu__list-item"><aaria-current="page"class="menu__link menu__link--active active"href="/docs/routing">Routing</a></li></ul></nav></div></aside><mainclass="docMainContainer_3ufF"><divclass="container padding-top--md padding-bottom--lg docItemWrapper_3FMP"><divclass="row"><divclass="col docItemCol_3FnS"><divclass="docItemContainer_33ec"><article><divclass="markdown"><header><h1class="h1Heading_27L5">Routing</h1></header><p>HTTP routing consists of binding an HTTP route to a C++ callback. A special component called an HTTP router will be in charge of dispatching HTTP requests to the right C++ callback. A route is composed of an HTTP verb associated to a resource:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly javascript"><pretabindex="0"class="prism-code language-javascript codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token constant"style="color:rgb(130, 170, 255)">GET</span><spanclass="token plain"></span><spanclass="token operator"style="color:rgb(137, 221, 255)">/</span><spanclass="token plain">users</span><spanclass="token operator"style="color:rgb(137, 221, 255)">/</span><spanclass="token number"style="color:rgb(247, 140, 108)">1</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>Here, <code>GET</code> is the verb and <code>/users/1</code> is the associated resource.</p><h2><aaria-hidden="true"tabindex="-1"class="anchor enhancedAnchor_2LWZ"id="http-methods"></a>HTTP methods<aclass="hash-link"href="#http-methods"title="Direct link to heading">#</a></h2><p>A bunch of HTTP methods (verbs) are supported by Pistache:</p><ul><li><em>GET</em>: The <code>GET</code> method is used by the client (e.g browser) to retrieve a resource identified by an URI. For example, to retrieve an user identified by an id, a client will issue a <code>GET</code> to the <code>/users/:id</code> Request-URI.</li><li><em>POST</em>: the <code>POST</code> method is used to post or send new information to a certain resource. The server will then read and store the data associated to the request. <code>POST</code> is a common way of transmitting data from an HTML form. <code>POST</code> can also be used to create a new resource or update information of an existing resource. For example, to create a new user, a client will issue a <code>POST</code> to the <code>/users</code> path with the data of the user to create in its body.</li><li><em>PUT</em>: <code>PUT</code> is very similar to <code>POST</code> except that <code>PUT</code> is idempotent, meaning that two requests to the same Request-URI with the same identical content should have the same effect and should produce the same result.</li><li><em>DELETE</em>: the <code>DELETE</code> method is used to delete a resource associated to a given Request-URI. For example, to remove an user, a client might issue a <code>DELETE</code> call to the <code>/users/:id</code> Request-URI.</li></ul><p>To sum up, <code>POST</code> and <code>PUT</code> are used to Create and/or Update, <code>GET</code> is used to Read and <code>DELETE</code> is used to Delete information.</p><h2><aaria-hidden="true"tabindex="-1"class="anchor enhancedAnchor_2LWZ"id="route-patterns"></a>Route patterns<aclass="hash-link"href="#route-patterns"title="Direct link to heading">#</a></h2><h3><aaria-hidden="true"tabindex="-1"class="anchor enhancedAnchor_2LWZ"id="static-routes"></a>Static routes<aclass="hash-link"href="#static-routes"title="Direct link to heading">#</a></h3><p>Static routes are the simplest ones as they do rely on dynamic parts of the Request-URI. For example <code>/users/all</code> is a static route that will exactly match the <code>/users/all</code> Request-URI.</p><h3><aaria-hidden="true"tabindex="-1"class="anchor enhancedAnchor_2LWZ"id="dynamic-routes"></a>Dynamic routes<aclass="hash-link"href="#dynamic-routes"title="Direct link to heading">#</a></h3><p>However, it is often useful to define routes that have dynamic parts. For example, to retrieve a specific user by its id, the id is needed to query the storage. Dynamic routes thus have parameters that are then matched one by one by the HTTP router. In a dynamic route, parameters are identified by a column <code>:</code></p><p><code>/users/:id</code></p><p>Here, <code>:id</code> is a dynamic parameter. When a request comes in, the router will try to match the <code>:id</code> parameter to the corresponding part of the request. For example, if the server receives a request to <code>/users/13</code>, the router will match the <code>13</code> value to the <code>:id</code> parameter.</p><p>Some parameters, like <code>:id</code> are named. However, Pistache also allows <em>splat</em> (wildcard) parameters, identified by a star <code>*</code>:</p><p><code>/link/*/to/*</code></p><h2><aaria-hidden="true"tabindex="-1"class="anchor enhancedAnchor_2LWZ"id="defining-routes"></a>Defining routes<aclass="hash-link"href="#defining-routes"title="Direct link to heading">#</a></h2><p>To define your routes, you first have to instantiate an HTTP router:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">Http</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Router router</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>Then, use the <code>Routes::<Method>()</code> functions to add some routes:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token class-name"style="color:rgb(255, 203, 107)">Routes</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token function"style="color:rgb(130, 170, 255)">Get</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain">router</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span><spanclass="token string"style="color:rgb(195, 232, 141)">"/users/all"</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span><spanclass="token class-name"style="color:rgb(255, 203, 107)">Routes</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token function"style="color:rgb(130, 170, 255)">bind</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token operator"style="color:rgb(137, 221, 255)">&</span><spanclass="token plain">UsersApi</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">getAllUsers</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span><spanclass="token keyword"style="font-style:italic">this</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token class-name"style="color:rgb(255, 203, 107)">Routes</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token function"style="color:rgb(130, 170, 255)">Post</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain">router</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span><spanclass="token string"style="color:rgb(195, 232, 141)">"/users/:id"</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span><spanclass="token class-name"style="color:rgb(255, 203, 107)">Routes</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token function"style="color:rgb(130, 170, 255)">bind</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token operator"style="color:rgb(137, 221, 255)">&</span><spanclass="token plain">UsersApi</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">getUserId</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span><spanclass="token keyword"style="font-style:italic">this</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token class-name"style="color:rgb(255, 203, 107)">Routes</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token function"style="color:rgb(130, 170, 255)">Get</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain">router</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span><spanclass="token string"style="color:rgb(195, 232, 141)">"/link/*/to/*"</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span><spanclass="token class-name"style="color:rgb(255, 203, 107)">Routes</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token function"style="color:rgb(130, 170, 255)">bind</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token operator"style="color:rgb(137, 221, 255)">&</span><spanclass="token plain">UsersApi</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">linkUsers</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"></span><spanclass="token keyword"style="font-style:italic">this</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p><code>Routes::bind</code> is a special function that will generate a corresponding C++ callback that will then be called by the router if a given route matches the Request-URI.</p><h3><aaria-hidden="true"tabindex="-1"class="anchor enhancedAnchor_2LWZ"id="callbacks"></a>Callbacks<aclass="hash-link"href="#callbacks"title="Direct link to heading">#</a></h3><p>A C++ callback associated to a route must have the following signature:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token keyword"style="font-style:italic">void</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token keyword"style="font-style:italic">const</span><spanclass="token plain"> Rest</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Request</span><spanclass="token operator"style="color:rgb(137, 221, 255)">&</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"> Http</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">ResponseWriter</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>A callback can either be a non-static free or member function. For member functions, a pointer to the corresponding instance must be passed to the Routes::bind function so that the router knows on which instance to invoke the member function.</p><p>The first parameter of the callback is <code>Rest::Request</code> and not an <code>Http::Request</code>. A <code>Rest::Request</code> is an <code>Http::Request</code> with additional functions. Named and splat parameters are for example retrieved through this object:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token keyword"style="font-style:italic">void</span><spanclass="token plain"></span><spanclass="token class-name"style="color:rgb(255, 203, 107)">UsersApi</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token function"style="color:rgb(130, 170, 255)">getUserId</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token keyword"style="font-style:italic">const</span><spanclass="token plain"> Rest</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Request</span><spanclass="token operator"style="color:rgb(137, 221, 255)">&</span><spanclass="token plain"> request</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"> Http</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">ResponseWriter response</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">{</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token keyword"style="font-style:italic">auto</span><spanclass="token plain"> id </span><spanclass="token operator"style="color:rgb(137, 221, 255)">=</span><spanclass="token plain"> request</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token function"style="color:rgb(130, 170, 255)">param</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token string"style="color:rgb(195, 232, 141)">":id"</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token plain">as</span><spanclass="token operator"style="color:rgb(137, 221, 255)"><</span><spanclass="token keyword"style="font-style:italic">int</span><spanclass="token operator"style="color:rgb(137, 221, 255)">></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token comment"style="color:rgb(105, 112, 152);font-style:italic">// ...</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">}</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"style="display:inline-block">
</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token keyword"style="font-style:italic">void</span><spanclass="token plain"></span><spanclass="token class-name"style="color:rgb(255, 203, 107)">UsersApi</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token function"style="color:rgb(130, 170, 255)">linkUsers</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token keyword"style="font-style:italic">const</span><spanclass="token plain"> Rest</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Request</span><spanclass="token operator"style="color:rgb(137, 221, 255)">&</span><spanclass="token plain"> request</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"> Http</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">ResponseWriter response</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">{</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token keyword"style="font-style:italic">auto</span><spanclass="token plain"> u1 </span><spanclass="token operator"style="color:rgb(137, 221, 255)">=</span><spanclass="token plain"> request</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token function"style="color:rgb(130, 170, 255)">splatAt</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token number"style="color:rgb(247, 140, 108)">0</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token plain">as</span><spanclass="token operator"style="color:rgb(137, 221, 255)"><</span><spanclass="token plain">std</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">string</span><spanclass="token operator"style="color:rgb(137, 221, 255)">></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token keyword"style="font-style:italic">auto</span><spanclass="token plain"> u2 </span><spanclass="token operator"style="color:rgb(137, 221, 255)">=</span><spanclass="token plain"> request</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token function"style="color:rgb(130, 170, 255)">splatAt</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token number"style="color:rgb(247, 140, 108)">1</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token plain">as</span><spanclass="token operator"style="color:rgb(137, 221, 255)"><</span><spanclass="token plain">std</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">string</span><spanclass="token operator"style="color:rgb(137, 221, 255)">></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token comment"style="color:rgb(105, 112, 152);font-style:italic">// ...</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">}</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>As you can see, parameters are also typed. To cast a parameter to the appropriate type, use the <code>as<T></code> member template.</p><divclass="admonition admonition-note alert alert--secondary"><divclass="admonition-heading"><h5><spanclass="admonition-icon"><svgxmlns="http://www.w3.org/2000/svg"width="14"height="16"viewBox="0 0 14 16"><pathfill-rule="evenodd"d="M6.3 5.69a.942.942 0 0 1-.28-.7c0-.28.09-.52.28-.7.19-.18.42-.28.7-.28.28 0 .52.09.7.28.18.19.28.42.28.7 0 .28-.09.52-.28.7a1 1 0 0 1-.7.3c-.28 0-.52-.11-.7-.3zM8 7.99c-.02-.25-.11-.48-.31-.69-.2-.19-.42-.3-.69-.31H6c-.27.02-.48.13-.69.31-.2.2-.3.44-.31.69h1v3c.02.27.11.5.31.69.2.2.42.31.69.31h1c.27 0 .48-.11.69-.31.2-.19.3-.42.31-.69H8V7.98v.01zM7 2.3c-3.14 0-5.7 2.54-5.7 5.68 0 3.14 2.56 5.7 5.7 5.7s5.7-2.55 5.7-5.7c0-3.15-2.56-5.69-5.7-5.69v.01zM7 .98c3.86 0 7 3.14 7 7s-3.14 7-7 7-7-3.12-7-7 3.14-7 7-7z"></path></svg></span>Cast safety</h5></div><divclass="admonition-content"><p>An exception will be thrown if the parameter can not be casted to the right type</p></div></div><h3><aaria-hidden="true"tabindex="-1"class="anchor enhancedAnchor_2LWZ"id="installing-the-handler"></a>Installing the handler<aclass="hash-link"href="#installing-the-handler"title="Direct link to heading">#</a></h3><p>Once the routes have been defined, the final <code>Http::Handler</code> must be set to the HTTP Endpoint. To retrieve the handler, just call the <code>handler()</code> member function on the router object:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">endpoint</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token function"style="color:rgb(130, 170, 255)">setHandler</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain">router</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token function"style="color:rgb(130, 170, 255)">handler</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div></div><footerclass="row docusaurus-mt-lg"><divclass="col"><ahref="https://github.com/pistacheio/pistache/edit/master/pistache.io/docs/routing.md"target="_blank"rel="noreferrer noopener"><svgfill="currentColor"height="20"width="20"viewBox="0 0 40 40"class="iconEdit_2_ui"aria-hidden="true"><g><pathd="m34.5 11.7l-3 3.1-6.3-6.3 3.1-3q0.5-0.5 1.2-0.5t1.1 0.5l3.9 3.9q0.5 0.4 0.5 1.1t-0.5 1.2z m-29.5 17.1l18.4-18.5 6.3 6.3-18.4 18.4h-6.3v-6.2z"></path></g></svg>Edit this page</a></div><divclass="col lastUpdated_3DPF"></div></footer></article><navclass="pagination-nav docusaurus-mt-lg"aria-label="Docs pages navigation"><divclass="pagination-nav__item"><aclass="pagination-nav__link"href="/docs/rest-description"><divclass="pagination-nav__sublabel">Previous</div><divclass="pagination-nav__label">« REST description</div></a></div><divclass="pagination-nav__item pagination-nav__item--next"></div></nav></div></div><divclass="col col--3"><divclass="tableOfContents_35-E thin-scrollbar"><ulclass="table-of-contents table-of-contents__left-border"><li><ahref="#http-methods"class="table-of-contents__link">HTTP methods</a></li><li><ahref="#route-patterns"class="table-of-contents__link">Route patterns</a><ul><li><ahref="#static-routes"class="table-of-contents__link">Static routes</a></li><li><ahref="#dynamic-routes"class="table-of-contents__link">Dynamic routes</a></li></ul></li><li><ahref="#defining-routes"class="table-of-contents__link">Defining routes</a><ul><li><ahref="#callbacks"class="table-of-contents__link">Callbacks</a></li><li><ahref="#installing-the-handler"class="table-of-contents__link">Installing the handler</a></li></ul></li></ul></div></div></div></div></main></div></div><footerclass="footer footer--dark"><divclass="container"><divclass="row footer__links"><divclass="col footer__col"><divclass="footer__title">Docs</div><ulclass="footer__items"><liclass="footer__item"><aclass="footer__link-item"href="/docs/">Quickstart</a></li><liclass="footer__item"><aclass="footer__link-item"href="/docs/http-handler/">User guide</a></li></ul></div><divclass="col footer__col"><divclass="footer__title">Community</div><ulclass="footer__items"><liclass="footer__item"><ahref="https://stackoverflow.com/questions/tagged/pistache"target="_blank"rel="noopener noreferrer"class="footer__link-item">Stack Overflow</a></li><liclass="footer__item"><ahref="https://web.libera.chat/#pistache"target="_blank"rel="noopener noreferrer"class="footer__link-item">#pistache on Libera.Chat</a></li></ul></div><divclass="col footer__col"><divclass="footer__title">More</div><ulclass="footer__items"><liclass="footer__item"><ahref="https://github.com/pistacheio/pistache"target="_blank"rel="noopener noreferrer"class="footer__link-item">GitHub</a></li></ul></div></div><divclass="footer__bottom text--center"><divclass="footer__copyright">Pistache, 2015 - 2021</div></div></div></footer></div>
</span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token keyword"style="font-style:italic">void</span><spanclass="token plain"></span><spanclass="token class-name"style="color:rgb(255, 203, 107)">UsersApi</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token function"style="color:rgb(130, 170, 255)">linkUsers</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token keyword"style="font-style:italic">const</span><spanclass="token plain"> Rest</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">Request</span><spanclass="token operator"style="color:rgb(137, 221, 255)">&</span><spanclass="token plain"> request</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">,</span><spanclass="token plain"> Http</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">ResponseWriter response</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">{</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token keyword"style="font-style:italic">auto</span><spanclass="token plain"> u1 </span><spanclass="token operator"style="color:rgb(137, 221, 255)">=</span><spanclass="token plain"> request</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token function"style="color:rgb(130, 170, 255)">splatAt</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token number"style="color:rgb(247, 140, 108)">0</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token plain">as</span><spanclass="token operator"style="color:rgb(137, 221, 255)"><</span><spanclass="token plain">std</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">string</span><spanclass="token operator"style="color:rgb(137, 221, 255)">></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token keyword"style="font-style:italic">auto</span><spanclass="token plain"> u2 </span><spanclass="token operator"style="color:rgb(137, 221, 255)">=</span><spanclass="token plain"> request</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token function"style="color:rgb(130, 170, 255)">splatAt</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token number"style="color:rgb(247, 140, 108)">1</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token plain">as</span><spanclass="token operator"style="color:rgb(137, 221, 255)"><</span><spanclass="token plain">std</span><spanclass="token operator"style="color:rgb(137, 221, 255)">::</span><spanclass="token plain">string</span><spanclass="token operator"style="color:rgb(137, 221, 255)">></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token comment"style="color:rgb(105, 112, 152);font-style:italic">// ...</span><spanclass="token plain"></span></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain"></span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">}</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div><p>As you can see, parameters are also typed. To cast a parameter to the appropriate type, use the <code>as<T></code> member template.</p><divclass="admonition admonition-note alert alert--secondary"><divclass="admonition-heading"><h5><spanclass="admonition-icon"><svgxmlns="http://www.w3.org/2000/svg"width="14"height="16"viewBox="0 0 14 16"><pathfill-rule="evenodd"d="M6.3 5.69a.942.942 0 0 1-.28-.7c0-.28.09-.52.28-.7.19-.18.42-.28.7-.28.28 0 .52.09.7.28.18.19.28.42.28.7 0 .28-.09.52-.28.7a1 1 0 0 1-.7.3c-.28 0-.52-.11-.7-.3zM8 7.99c-.02-.25-.11-.48-.31-.69-.2-.19-.42-.3-.69-.31H6c-.27.02-.48.13-.69.31-.2.2-.3.44-.31.69h1v3c.02.27.11.5.31.69.2.2.42.31.69.31h1c.27 0 .48-.11.69-.31.2-.19.3-.42.31-.69H8V7.98v.01zM7 2.3c-3.14 0-5.7 2.54-5.7 5.68 0 3.14 2.56 5.7 5.7 5.7s5.7-2.55 5.7-5.7c0-3.15-2.56-5.69-5.7-5.69v.01zM7 .98c3.86 0 7 3.14 7 7s-3.14 7-7 7-7-3.12-7-7 3.14-7 7-7z"></path></svg></span>Cast safety</h5></div><divclass="admonition-content"><p>An exception will be thrown if the parameter can not be casted to the right type</p></div></div><h3><aaria-hidden="true"tabindex="-1"class="anchor enhancedAnchor_2LWZ"id="installing-the-handler"></a>Installing the handler<aclass="hash-link"href="#installing-the-handler"title="Direct link to heading">#</a></h3><p>Once the routes have been defined, the final <code>Http::Handler</code> must be set to the HTTP Endpoint. To retrieve the handler, just call the <code>handler()</code> member function on the router object:</p><divclass="codeBlockContainer_K1bP"><divclass="codeBlockContent_hGly cpp"><pretabindex="0"class="prism-code language-cpp codeBlock_23N8 thin-scrollbar"style="color:#bfc7d5;background-color:#292d3e"><codeclass="codeBlockLines_39YC"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">endpoint</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token function"style="color:rgb(130, 170, 255)">setHandler</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token plain">router</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">.</span><spanclass="token function"style="color:rgb(130, 170, 255)">handler</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">(</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">)</span><spanclass="token punctuation"style="color:rgb(199, 146, 234)">;</span></span></code></pre><buttontype="button"aria-label="Copy code to clipboard"class="copyButton_Ue-o clean-btn">Copy</button></div></div></div><footerclass="row docusaurus-mt-lg"><divclass="col"><ahref="https://github.com/pistacheio/pistache/edit/master/pistache.io/docs/routing.md"target="_blank"rel="noreferrer noopener"><svgfill="currentColor"height="20"width="20"viewBox="0 0 40 40"class="iconEdit_2_ui"aria-hidden="true"><g><pathd="m34.5 11.7l-3 3.1-6.3-6.3 3.1-3q0.5-0.5 1.2-0.5t1.1 0.5l3.9 3.9q0.5 0.4 0.5 1.1t-0.5 1.2z m-29.5 17.1l18.4-18.5 6.3 6.3-18.4 18.4h-6.3v-6.2z"></path></g></svg>Edit this page</a></div><divclass="col lastUpdated_3DPF"></div></footer></article><navclass="pagination-nav docusaurus-mt-lg"aria-label="Docs pages navigation"><divclass="pagination-nav__item"><aclass="pagination-nav__link"href="/docs/rest-description"><divclass="pagination-nav__sublabel">Previous</div><divclass="pagination-nav__label">« REST description</div></a></div><divclass="pagination-nav__item pagination-nav__item--next"></div></nav></div></div><divclass="col col--3"><divclass="tableOfContents_35-E thin-scrollbar"><ulclass="table-of-contents table-of-contents__left-border"><li><ahref="#http-methods"class="table-of-contents__link">HTTP methods</a></li><li><ahref="#route-patterns"class="table-of-contents__link">Route patterns</a><ul><li><ahref="#static-routes"class="table-of-contents__link">Static routes</a></li><li><ahref="#dynamic-routes"class="table-of-contents__link">Dynamic routes</a></li></ul></li><li><ahref="#defining-routes"class="table-of-contents__link">Defining routes</a><ul><li><ahref="#callbacks"class="table-of-contents__link">Callbacks</a></li><li><ahref="#installing-the-handler"class="table-of-contents__link">Installing the handler</a></li></ul></li></ul></div></div></div></div></main></div></div><footerclass="footer footer--dark"><divclass="container"><divclass="row footer__links"><divclass="col footer__col"><divclass="footer__title">Docs</div><ulclass="footer__items"><liclass="footer__item"><aclass="footer__link-item"href="/docs/">Quickstart</a></li><liclass="footer__item"><aclass="footer__link-item"href="/docs/http-handler/">User guide</a></li></ul></div><divclass="col footer__col"><divclass="footer__title">Community</div><ulclass="footer__items"><liclass="footer__item"><ahref="https://stackoverflow.com/questions/tagged/pistache"target="_blank"rel="noopener noreferrer"class="footer__link-item">Stack Overflow</a></li><liclass="footer__item"><ahref="https://web.libera.chat/#pistache"target="_blank"rel="noopener noreferrer"class="footer__link-item">#pistache on Libera.Chat</a></li></ul></div><divclass="col footer__col"><divclass="footer__title">More</div><ulclass="footer__items"><liclass="footer__item"><ahref="https://github.com/pistacheio/pistache"target="_blank"rel="noopener noreferrer"class="footer__link-item">GitHub</a></li></ul></div></div><divclass="footer__bottom text--center"><divclass="footer__copyright">Pistache, 2015 - 2021</div></div></div></footer></div>