mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-04-12 19:58:29 +00:00
237 lines
14 KiB
HTML
237 lines
14 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en" data-base-path="/cpp-httplib">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<title>Getting Started - cpp-httplib</title>
|
|
<link rel="icon" type="image/svg+xml" href="/cpp-httplib/favicon.svg">
|
|
<link rel="stylesheet" href="/cpp-httplib/css/main.css">
|
|
<script>
|
|
(function() {
|
|
var t = localStorage.getItem('preferred-theme');
|
|
if (!t) t = window.matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark';
|
|
if (t === 'light') document.documentElement.setAttribute('data-theme', 'light');
|
|
})();
|
|
</script>
|
|
</head>
|
|
<body>
|
|
<header class="header">
|
|
<div class="header-inner">
|
|
<a href="/cpp-httplib/en/" class="header-title">cpp-httplib <span style="font-size:0.75em;font-weight:normal;margin-left:4px">v0.36.0</span></a>
|
|
<div class="header-spacer"></div>
|
|
<nav class="header-nav">
|
|
<a href="/cpp-httplib/en/">
|
|
<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>
|
|
Home
|
|
</a>
|
|
|
|
|
|
<a href="/cpp-httplib/en/tour/">
|
|
<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>
|
|
Tour
|
|
</a>
|
|
|
|
|
|
|
|
<a href="https://github.com/yhirose/cpp-httplib" aria-label="GitHub">
|
|
<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>
|
|
GitHub
|
|
</a>
|
|
|
|
|
|
</nav>
|
|
<div class="header-tools">
|
|
<button class="search-btn" aria-label="Search (⌘K)">
|
|
<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>
|
|
</button>
|
|
<button class="theme-toggle" aria-label="Toggle theme"></button>
|
|
|
|
<div class="lang-selector">
|
|
<button class="lang-btn" aria-label="Language">
|
|
<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>
|
|
EN
|
|
</button>
|
|
<ul class="lang-popup">
|
|
|
|
<li><a href="#" data-lang="en">EN</a></li>
|
|
|
|
<li><a href="#" data-lang="ja">JA</a></li>
|
|
|
|
</ul>
|
|
</div>
|
|
|
|
</div>
|
|
<button class="sidebar-toggle" aria-label="Menu">☰</button>
|
|
</div>
|
|
</header>
|
|
|
|
|
|
|
|
<div class="layout has-sidebar">
|
|
|
|
<aside class="sidebar">
|
|
<nav class="sidebar-nav">
|
|
|
|
<div class="nav-section">
|
|
<a href="/cpp-httplib/en/tour/" class="nav-section-title active">A Tour of cpp-httplib</a>
|
|
|
|
<ul class="nav-list">
|
|
|
|
<li><a href="/cpp-httplib/en/tour/01-getting-started/" class="active">Getting Started</a></li>
|
|
|
|
<li><a href="/cpp-httplib/en/tour/02-basic-client/" class="">Basic Client</a></li>
|
|
|
|
<li><a href="/cpp-httplib/en/tour/03-basic-server/" class="">Basic Server</a></li>
|
|
|
|
<li><a href="/cpp-httplib/en/tour/04-static-file-server/" class="">Static File Server</a></li>
|
|
|
|
<li><a href="/cpp-httplib/en/tour/05-tls-setup/" class="">TLS Setup</a></li>
|
|
|
|
<li><a href="/cpp-httplib/en/tour/06-https-client/" class="">HTTPS Client</a></li>
|
|
|
|
<li><a href="/cpp-httplib/en/tour/07-https-server/" class="">HTTPS Server</a></li>
|
|
|
|
<li><a href="/cpp-httplib/en/tour/08-websocket/" class="">WebSocket</a></li>
|
|
|
|
<li><a href="/cpp-httplib/en/tour/09-whats-next/" class="">What's Next</a></li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
</nav>
|
|
</aside>
|
|
<main class="content">
|
|
<article>
|
|
<h1>Getting Started</h1>
|
|
<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>
|
|
<h2>Getting httplib.h</h2>
|
|
<p>You can download it directly from GitHub. Always use the latest version.</p>
|
|
<div class="code-block-wrapper"><div data-code-theme="dark"><pre style="background-color:#2b303b;">
|
|
<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
|
|
</span></pre>
|
|
</div><div data-code-theme="light"><pre style="background-color:#ffffff;">
|
|
<span style="color:#323232;">curl -LO https://github.com/yhirose/cpp-httplib/raw/refs/tags/latest/httplib.h
|
|
</span></pre>
|
|
</div></div>
|
|
<p>Place the downloaded <code>httplib.h</code> in your project directory and you're good to go.</p>
|
|
<h2>Setting Up Your Compiler</h2>
|
|
<table><thead><tr><th>OS</th><th>Development Environment</th><th>Setup</th></tr></thead><tbody>
|
|
<tr><td>macOS</td><td>Apple Clang</td><td>Xcode Command Line Tools (<code>xcode-select --install</code>)</td></tr>
|
|
<tr><td>Ubuntu</td><td>clang++ or g++</td><td><code>apt install clang</code> or <code>apt install g++</code></td></tr>
|
|
<tr><td>Windows</td><td>MSVC</td><td>Visual Studio 2022 or later (install with C++ components)</td></tr>
|
|
</tbody></table>
|
|
<h2>Hello World Server</h2>
|
|
<p>Save the following code as <code>server.cpp</code>.</p>
|
|
<div class="code-block-wrapper"><div data-code-theme="dark"><pre style="background-color:#2b303b;">
|
|
<span style="color:#b48ead;">#include </span><span style="color:#c0c5ce;">"</span><span style="color:#a3be8c;">httplib.h</span><span style="color:#c0c5ce;">"
|
|
</span><span style="color:#c0c5ce;">
|
|
</span><span style="color:#b48ead;">int </span><span style="color:#8fa1b3;">main</span><span style="color:#c0c5ce;">() {
|
|
</span><span style="color:#c0c5ce;"> httplib::Server svr;
|
|
</span><span style="color:#c0c5ce;">
|
|
</span><span style="color:#c0c5ce;"> svr.</span><span style="color:#8fa1b3;">Get</span><span style="color:#c0c5ce;">("</span><span style="color:#a3be8c;">/</span><span style="color:#c0c5ce;">", [](</span><span style="color:#b48ead;">const</span><span style="color:#c0c5ce;"> httplib::Request&, httplib::Response& res) {
|
|
</span><span style="color:#c0c5ce;"> res.</span><span style="color:#8fa1b3;">set_content</span><span style="color:#c0c5ce;">("</span><span style="color:#a3be8c;">Hello, World!</span><span style="color:#c0c5ce;">", "</span><span style="color:#a3be8c;">text/plain</span><span style="color:#c0c5ce;">");
|
|
</span><span style="color:#c0c5ce;"> });
|
|
</span><span style="color:#c0c5ce;">
|
|
</span><span style="color:#c0c5ce;"> svr.</span><span style="color:#8fa1b3;">listen</span><span style="color:#c0c5ce;">("</span><span style="color:#a3be8c;">0.0.0.0</span><span style="color:#c0c5ce;">", </span><span style="color:#d08770;">8080</span><span style="color:#c0c5ce;">);
|
|
</span><span style="color:#c0c5ce;">}
|
|
</span></pre>
|
|
</div><div data-code-theme="light"><pre style="background-color:#ffffff;">
|
|
<span style="font-weight:bold;color:#a71d5d;">#include </span><span style="color:#183691;">"httplib.h"
|
|
</span><span style="color:#323232;">
|
|
</span><span style="font-weight:bold;color:#a71d5d;">int </span><span style="font-weight:bold;color:#795da3;">main</span><span style="color:#323232;">() {
|
|
</span><span style="color:#323232;"> httplib::Server svr;
|
|
</span><span style="color:#323232;">
|
|
</span><span style="color:#323232;"> svr.Get(</span><span style="color:#183691;">"/"</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;">&</span><span style="color:#323232;">, httplib::Response</span><span style="font-weight:bold;color:#a71d5d;">&</span><span style="color:#323232;"> res) {
|
|
</span><span style="color:#323232;"> res.set_content(</span><span style="color:#183691;">"Hello, World!"</span><span style="color:#323232;">, </span><span style="color:#183691;">"text/plain"</span><span style="color:#323232;">);
|
|
</span><span style="color:#323232;"> });
|
|
</span><span style="color:#323232;">
|
|
</span><span style="color:#323232;"> svr.listen(</span><span style="color:#183691;">"0.0.0.0"</span><span style="color:#323232;">, </span><span style="color:#0086b3;">8080</span><span style="color:#323232;">);
|
|
</span><span style="color:#323232;">}
|
|
</span></pre>
|
|
</div></div>
|
|
<p>In just a few lines, you have a server that responds to HTTP requests.</p>
|
|
<h2>Compiling and Running</h2>
|
|
<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>
|
|
<div class="code-block-wrapper"><div data-code-theme="dark"><pre style="background-color:#2b303b;">
|
|
<span style="color:#65737e;"># macOS
|
|
</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
|
|
</span><span style="color:#c0c5ce;">
|
|
</span><span style="color:#65737e;"># Linux
|
|
</span><span style="color:#65737e;"># `-pthread`: cpp-httplib uses threads internally
|
|
</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
|
|
</span><span style="color:#c0c5ce;">
|
|
</span><span style="color:#65737e;"># Windows (Developer Command Prompt)
|
|
</span><span style="color:#65737e;"># `/EHsc`: Enable C++ exception handling
|
|
</span><span style="color:#8fa1b3;">cl</span><span style="color:#c0c5ce;"> /EHsc /std:c++17 server.cpp
|
|
</span></pre>
|
|
</div><div data-code-theme="light"><pre style="background-color:#ffffff;">
|
|
<span style="font-style:italic;color:#969896;"># macOS
|
|
</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
|
|
</span><span style="color:#323232;">
|
|
</span><span style="font-style:italic;color:#969896;"># Linux
|
|
</span><span style="font-style:italic;color:#969896;"># `-pthread`: cpp-httplib uses threads internally
|
|
</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
|
|
</span><span style="color:#323232;">
|
|
</span><span style="font-style:italic;color:#969896;"># Windows (Developer Command Prompt)
|
|
</span><span style="font-style:italic;color:#969896;"># `/EHsc`: Enable C++ exception handling
|
|
</span><span style="color:#323232;">cl /EHsc /std:c++17 server.cpp
|
|
</span></pre>
|
|
</div></div>
|
|
<p>Once it compiles, run it.</p>
|
|
<div class="code-block-wrapper"><div data-code-theme="dark"><pre style="background-color:#2b303b;">
|
|
<span style="color:#65737e;"># macOS / Linux
|
|
</span><span style="color:#8fa1b3;">./server
|
|
</span><span style="color:#c0c5ce;">
|
|
</span><span style="color:#65737e;"># Windows
|
|
</span><span style="color:#8fa1b3;">server.exe
|
|
</span></pre>
|
|
</div><div data-code-theme="light"><pre style="background-color:#ffffff;">
|
|
<span style="font-style:italic;color:#969896;"># macOS / Linux
|
|
</span><span style="color:#323232;">./server
|
|
</span><span style="color:#323232;">
|
|
</span><span style="font-style:italic;color:#969896;"># Windows
|
|
</span><span style="color:#323232;">server.exe
|
|
</span></pre>
|
|
</div></div>
|
|
<p>Open <code>http://localhost:8080</code> in your browser. If you see "Hello, World!", you're all set.</p>
|
|
<p>You can also verify with <code>curl</code>.</p>
|
|
<div class="code-block-wrapper"><div data-code-theme="dark"><pre style="background-color:#2b303b;">
|
|
<span style="color:#8fa1b3;">curl</span><span style="color:#c0c5ce;"> http://localhost:8080/
|
|
</span><span style="color:#65737e;"># Hello, World!
|
|
</span></pre>
|
|
</div><div data-code-theme="light"><pre style="background-color:#ffffff;">
|
|
<span style="color:#323232;">curl http://localhost:8080/
|
|
</span><span style="font-style:italic;color:#969896;"># Hello, World!
|
|
</span></pre>
|
|
</div></div>
|
|
<p>To stop the server, press <code>Ctrl+C</code> in your terminal.</p>
|
|
<h2>Next Steps</h2>
|
|
<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>
|
|
<p><strong>Next:</strong> <a href="../02-basic-client">Basic Client</a></p>
|
|
|
|
</article>
|
|
</main>
|
|
|
|
</div>
|
|
|
|
<footer class="footer">
|
|
© 2026 Yuji Hirose. All rights reserved.
|
|
</footer>
|
|
|
|
<!-- Search modal -->
|
|
<div class="search-overlay" id="search-overlay">
|
|
<div class="search-modal">
|
|
<div class="search-input-wrap">
|
|
<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>
|
|
<input type="text" id="search-input" placeholder="Search..." autocomplete="off" spellcheck="false">
|
|
<kbd class="search-esc">ESC</kbd>
|
|
</div>
|
|
<ul class="search-results" id="search-results"></ul>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="/cpp-httplib/js/main.js"></script>
|
|
</body>
|
|
</html>
|