index.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <!DOCTYPE html>
  2. <html lang="en" data-base-path="&#x2F;cpp-httplib">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>Getting Started - cpp-httplib</title>
  7. <link rel="icon" type="image/svg+xml" href="&#x2F;cpp-httplib/favicon.svg">
  8. <link rel="stylesheet" href="&#x2F;cpp-httplib/css/main.css">
  9. <script>
  10. (function() {
  11. var t = localStorage.getItem('preferred-theme');
  12. if (!t) t = window.matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark';
  13. if (t === 'light') document.documentElement.setAttribute('data-theme', 'light');
  14. })();
  15. </script>
  16. </head>
  17. <body>
  18. <header class="header">
  19. <div class="header-inner">
  20. <a href="&#x2F;cpp-httplib&#x2F;en/" class="header-title">cpp-httplib <span style="font-size:0.75em;font-weight:normal;margin-left:4px">v0.36.0</span></a>
  21. <div class="header-spacer"></div>
  22. <nav class="header-nav">
  23. <a href="&#x2F;cpp-httplib&#x2F;en/">
  24. <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/><polyline points="9 22 9 12 15 12 15 22"/></svg>
  25. Home
  26. </a>
  27. <a href="&#x2F;cpp-httplib&#x2F;en/tour&#x2F;">
  28. <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><polygon points="16.24 7.76 14.12 14.12 7.76 16.24 9.88 9.88 16.24 7.76"/></svg>
  29. Tour
  30. </a>
  31. <a href="https:&#x2F;&#x2F;github.com&#x2F;yhirose&#x2F;cpp-httplib" aria-label="GitHub">
  32. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M9 19c-5 1.5-5-2.5-7-3m14 6v-3.87a3.37 3.37 0 0 0-.94-2.61c3.14-.35 6.44-1.54 6.44-7A5.44 5.44 0 0 0 20 4.77 5.07 5.07 0 0 0 19.91 1S18.73.65 16 2.48a13.38 13.38 0 0 0-7 0C6.27.65 5.09 1 5.09 1A5.07 5.07 0 0 0 5 4.77a5.44 5.44 0 0 0-1.5 3.78c0 5.42 3.3 6.61 6.44 7A3.37 3.37 0 0 0 9 18.13V22"/></svg>
  33. GitHub
  34. </a>
  35. </nav>
  36. <div class="header-tools">
  37. <button class="search-btn" aria-label="Search (⌘K)">
  38. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></svg>
  39. </button>
  40. <button class="theme-toggle" aria-label="Toggle theme"></button>
  41. <div class="lang-selector">
  42. <button class="lang-btn" aria-label="Language">
  43. <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><line x1="2" y1="12" x2="22" y2="12"/><path d="M12 2a15.3 15.3 0 0 1 4 10 15.3 15.3 0 0 1-4 10 15.3 15.3 0 0 1-4-10 15.3 15.3 0 0 1 4-10z"/></svg>
  44. EN
  45. </button>
  46. <ul class="lang-popup">
  47. <li><a href="#" data-lang="en">EN</a></li>
  48. <li><a href="#" data-lang="ja">JA</a></li>
  49. </ul>
  50. </div>
  51. </div>
  52. <button class="sidebar-toggle" aria-label="Menu">&#9776;</button>
  53. </div>
  54. </header>
  55. <div class="layout has-sidebar">
  56. <aside class="sidebar">
  57. <nav class="sidebar-nav">
  58. <div class="nav-section">
  59. <a href="&#x2F;cpp-httplib&#x2F;en&#x2F;tour&#x2F;" class="nav-section-title active">A Tour of cpp-httplib</a>
  60. <ul class="nav-list">
  61. <li><a href="&#x2F;cpp-httplib&#x2F;en&#x2F;tour&#x2F;01-getting-started&#x2F;" class="active">Getting Started</a></li>
  62. <li><a href="&#x2F;cpp-httplib&#x2F;en&#x2F;tour&#x2F;02-basic-client&#x2F;" class="">Basic Client</a></li>
  63. <li><a href="&#x2F;cpp-httplib&#x2F;en&#x2F;tour&#x2F;03-basic-server&#x2F;" class="">Basic Server</a></li>
  64. <li><a href="&#x2F;cpp-httplib&#x2F;en&#x2F;tour&#x2F;04-static-file-server&#x2F;" class="">Static File Server</a></li>
  65. <li><a href="&#x2F;cpp-httplib&#x2F;en&#x2F;tour&#x2F;05-tls-setup&#x2F;" class="">TLS Setup</a></li>
  66. <li><a href="&#x2F;cpp-httplib&#x2F;en&#x2F;tour&#x2F;06-https-client&#x2F;" class="">HTTPS Client</a></li>
  67. <li><a href="&#x2F;cpp-httplib&#x2F;en&#x2F;tour&#x2F;07-https-server&#x2F;" class="">HTTPS Server</a></li>
  68. <li><a href="&#x2F;cpp-httplib&#x2F;en&#x2F;tour&#x2F;08-websocket&#x2F;" class="">WebSocket</a></li>
  69. <li><a href="&#x2F;cpp-httplib&#x2F;en&#x2F;tour&#x2F;09-whats-next&#x2F;" class="">What&#x27;s Next</a></li>
  70. </ul>
  71. </div>
  72. </nav>
  73. </aside>
  74. <main class="content">
  75. <article>
  76. <h1>Getting Started</h1>
  77. <p>All you need to get started with cpp-httplib is <code>httplib.h</code> and a C++ compiler. Let's download the file and get a Hello World server running.</p>
  78. <h2>Getting httplib.h</h2>
  79. <p>You can download it directly from GitHub. Always use the latest version.</p>
  80. <div class="code-block-wrapper"><div data-code-theme="dark"><pre style="background-color:#2b303b;">
  81. <span style="color:#8fa1b3;">curl</span><span style="color:#bf616a;"> -LO</span><span style="color:#c0c5ce;"> https://github.com/yhirose/cpp-httplib/raw/refs/tags/latest/httplib.h
  82. </span></pre>
  83. </div><div data-code-theme="light"><pre style="background-color:#ffffff;">
  84. <span style="color:#323232;">curl -LO https://github.com/yhirose/cpp-httplib/raw/refs/tags/latest/httplib.h
  85. </span></pre>
  86. </div></div>
  87. <p>Place the downloaded <code>httplib.h</code> in your project directory and you're good to go.</p>
  88. <h2>Setting Up Your Compiler</h2>
  89. <table><thead><tr><th>OS</th><th>Development Environment</th><th>Setup</th></tr></thead><tbody>
  90. <tr><td>macOS</td><td>Apple Clang</td><td>Xcode Command Line Tools (<code>xcode-select --install</code>)</td></tr>
  91. <tr><td>Ubuntu</td><td>clang++ or g++</td><td><code>apt install clang</code> or <code>apt install g++</code></td></tr>
  92. <tr><td>Windows</td><td>MSVC</td><td>Visual Studio 2022 or later (install with C++ components)</td></tr>
  93. </tbody></table>
  94. <h2>Hello World Server</h2>
  95. <p>Save the following code as <code>server.cpp</code>.</p>
  96. <div class="code-block-wrapper"><div data-code-theme="dark"><pre style="background-color:#2b303b;">
  97. <span style="color:#b48ead;">#include </span><span style="color:#c0c5ce;">&quot;</span><span style="color:#a3be8c;">httplib.h</span><span style="color:#c0c5ce;">&quot;
  98. </span><span style="color:#c0c5ce;">
  99. </span><span style="color:#b48ead;">int </span><span style="color:#8fa1b3;">main</span><span style="color:#c0c5ce;">() {
  100. </span><span style="color:#c0c5ce;"> httplib::Server svr;
  101. </span><span style="color:#c0c5ce;">
  102. </span><span style="color:#c0c5ce;"> svr.</span><span style="color:#8fa1b3;">Get</span><span style="color:#c0c5ce;">(&quot;</span><span style="color:#a3be8c;">/</span><span style="color:#c0c5ce;">&quot;, [](</span><span style="color:#b48ead;">const</span><span style="color:#c0c5ce;"> httplib::Request&amp;, httplib::Response&amp; res) {
  103. </span><span style="color:#c0c5ce;"> res.</span><span style="color:#8fa1b3;">set_content</span><span style="color:#c0c5ce;">(&quot;</span><span style="color:#a3be8c;">Hello, World!</span><span style="color:#c0c5ce;">&quot;, &quot;</span><span style="color:#a3be8c;">text/plain</span><span style="color:#c0c5ce;">&quot;);
  104. </span><span style="color:#c0c5ce;"> });
  105. </span><span style="color:#c0c5ce;">
  106. </span><span style="color:#c0c5ce;"> svr.</span><span style="color:#8fa1b3;">listen</span><span style="color:#c0c5ce;">(&quot;</span><span style="color:#a3be8c;">0.0.0.0</span><span style="color:#c0c5ce;">&quot;, </span><span style="color:#d08770;">8080</span><span style="color:#c0c5ce;">);
  107. </span><span style="color:#c0c5ce;">}
  108. </span></pre>
  109. </div><div data-code-theme="light"><pre style="background-color:#ffffff;">
  110. <span style="font-weight:bold;color:#a71d5d;">#include </span><span style="color:#183691;">&quot;httplib.h&quot;
  111. </span><span style="color:#323232;">
  112. </span><span style="font-weight:bold;color:#a71d5d;">int </span><span style="font-weight:bold;color:#795da3;">main</span><span style="color:#323232;">() {
  113. </span><span style="color:#323232;"> httplib::Server svr;
  114. </span><span style="color:#323232;">
  115. </span><span style="color:#323232;"> svr.Get(</span><span style="color:#183691;">&quot;/&quot;</span><span style="color:#323232;">, [](</span><span style="font-weight:bold;color:#a71d5d;">const</span><span style="color:#323232;"> httplib::Request</span><span style="font-weight:bold;color:#a71d5d;">&amp;</span><span style="color:#323232;">, httplib::Response</span><span style="font-weight:bold;color:#a71d5d;">&amp;</span><span style="color:#323232;"> res) {
  116. </span><span style="color:#323232;"> res.set_content(</span><span style="color:#183691;">&quot;Hello, World!&quot;</span><span style="color:#323232;">, </span><span style="color:#183691;">&quot;text/plain&quot;</span><span style="color:#323232;">);
  117. </span><span style="color:#323232;"> });
  118. </span><span style="color:#323232;">
  119. </span><span style="color:#323232;"> svr.listen(</span><span style="color:#183691;">&quot;0.0.0.0&quot;</span><span style="color:#323232;">, </span><span style="color:#0086b3;">8080</span><span style="color:#323232;">);
  120. </span><span style="color:#323232;">}
  121. </span></pre>
  122. </div></div>
  123. <p>In just a few lines, you have a server that responds to HTTP requests.</p>
  124. <h2>Compiling and Running</h2>
  125. <p>The sample code in this tutorial is written in C++17 for cleaner, more concise code. cpp-httplib itself can compile with C++11 as well.</p>
  126. <div class="code-block-wrapper"><div data-code-theme="dark"><pre style="background-color:#2b303b;">
  127. <span style="color:#65737e;"># macOS
  128. </span><span style="color:#8fa1b3;">clang++</span><span style="color:#bf616a;"> -std</span><span style="color:#c0c5ce;">=c++17</span><span style="color:#bf616a;"> -o</span><span style="color:#c0c5ce;"> server server.cpp
  129. </span><span style="color:#c0c5ce;">
  130. </span><span style="color:#65737e;"># Linux
  131. </span><span style="color:#65737e;"># `-pthread`: cpp-httplib uses threads internally
  132. </span><span style="color:#8fa1b3;">clang++</span><span style="color:#bf616a;"> -std</span><span style="color:#c0c5ce;">=c++17</span><span style="color:#bf616a;"> -pthread -o</span><span style="color:#c0c5ce;"> server server.cpp
  133. </span><span style="color:#c0c5ce;">
  134. </span><span style="color:#65737e;"># Windows (Developer Command Prompt)
  135. </span><span style="color:#65737e;"># `/EHsc`: Enable C++ exception handling
  136. </span><span style="color:#8fa1b3;">cl</span><span style="color:#c0c5ce;"> /EHsc /std:c++17 server.cpp
  137. </span></pre>
  138. </div><div data-code-theme="light"><pre style="background-color:#ffffff;">
  139. <span style="font-style:italic;color:#969896;"># macOS
  140. </span><span style="color:#323232;">clang++ -std</span><span style="font-weight:bold;color:#a71d5d;">=</span><span style="color:#323232;">c++17 -o server server.cpp
  141. </span><span style="color:#323232;">
  142. </span><span style="font-style:italic;color:#969896;"># Linux
  143. </span><span style="font-style:italic;color:#969896;"># `-pthread`: cpp-httplib uses threads internally
  144. </span><span style="color:#323232;">clang++ -std</span><span style="font-weight:bold;color:#a71d5d;">=</span><span style="color:#323232;">c++17 -pthread -o server server.cpp
  145. </span><span style="color:#323232;">
  146. </span><span style="font-style:italic;color:#969896;"># Windows (Developer Command Prompt)
  147. </span><span style="font-style:italic;color:#969896;"># `/EHsc`: Enable C++ exception handling
  148. </span><span style="color:#323232;">cl /EHsc /std:c++17 server.cpp
  149. </span></pre>
  150. </div></div>
  151. <p>Once it compiles, run it.</p>
  152. <div class="code-block-wrapper"><div data-code-theme="dark"><pre style="background-color:#2b303b;">
  153. <span style="color:#65737e;"># macOS / Linux
  154. </span><span style="color:#8fa1b3;">./server
  155. </span><span style="color:#c0c5ce;">
  156. </span><span style="color:#65737e;"># Windows
  157. </span><span style="color:#8fa1b3;">server.exe
  158. </span></pre>
  159. </div><div data-code-theme="light"><pre style="background-color:#ffffff;">
  160. <span style="font-style:italic;color:#969896;"># macOS / Linux
  161. </span><span style="color:#323232;">./server
  162. </span><span style="color:#323232;">
  163. </span><span style="font-style:italic;color:#969896;"># Windows
  164. </span><span style="color:#323232;">server.exe
  165. </span></pre>
  166. </div></div>
  167. <p>Open <code>http://localhost:8080</code> in your browser. If you see "Hello, World!", you're all set.</p>
  168. <p>You can also verify with <code>curl</code>.</p>
  169. <div class="code-block-wrapper"><div data-code-theme="dark"><pre style="background-color:#2b303b;">
  170. <span style="color:#8fa1b3;">curl</span><span style="color:#c0c5ce;"> http://localhost:8080/
  171. </span><span style="color:#65737e;"># Hello, World!
  172. </span></pre>
  173. </div><div data-code-theme="light"><pre style="background-color:#ffffff;">
  174. <span style="color:#323232;">curl http://localhost:8080/
  175. </span><span style="font-style:italic;color:#969896;"># Hello, World!
  176. </span></pre>
  177. </div></div>
  178. <p>To stop the server, press <code>Ctrl+C</code> in your terminal.</p>
  179. <h2>Next Steps</h2>
  180. <p>Now you know the basics of running a server. Next, let's look at the client side. cpp-httplib also comes with HTTP client functionality.</p>
  181. <p><strong>Next:</strong> <a href="../02-basic-client">Basic Client</a></p>
  182. </article>
  183. </main>
  184. </div>
  185. <footer class="footer">
  186. © 2026 Yuji Hirose. All rights reserved.
  187. </footer>
  188. <!-- Search modal -->
  189. <div class="search-overlay" id="search-overlay">
  190. <div class="search-modal">
  191. <div class="search-input-wrap">
  192. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></svg>
  193. <input type="text" id="search-input" placeholder="Search..." autocomplete="off" spellcheck="false">
  194. <kbd class="search-esc">ESC</kbd>
  195. </div>
  196. <ul class="search-results" id="search-results"></ul>
  197. </div>
  198. </div>
  199. <script src="&#x2F;cpp-httplib/js/main.js"></script>
  200. </body>
  201. </html>