mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-04-11 19:28:30 +00:00
2.0 KiB
2.0 KiB
title, order, status
| title | order | status |
|---|---|---|
| S01. Register GET / POST / PUT / DELETE Handlers | 20 | draft |
With httplib::Server, you register a handler per HTTP method. Just pass a pattern and a lambda to Get(), Post(), Put(), or Delete().
Basic usage
#include <httplib.h>
int main() {
httplib::Server svr;
svr.Get("/hello", [](const httplib::Request &req, httplib::Response &res) {
res.set_content("Hello, World!", "text/plain");
});
svr.Post("/api/items", [](const httplib::Request &req, httplib::Response &res) {
// req.body holds the request body
res.status = 201;
res.set_content("Created", "text/plain");
});
svr.Put("/api/items/1", [](const httplib::Request &req, httplib::Response &res) {
res.set_content("Updated", "text/plain");
});
svr.Delete("/api/items/1", [](const httplib::Request &req, httplib::Response &res) {
res.status = 204;
});
svr.listen("0.0.0.0", 8080);
}
Handlers take (const Request&, Response&). Use res.set_content() to set the body and Content-Type, and res.status for the status code. listen() starts the server and blocks.
Read query parameters
svr.Get("/search", [](const httplib::Request &req, httplib::Response &res) {
auto q = req.get_param_value("q");
auto limit = req.get_param_value("limit");
res.set_content("q=" + q + ", limit=" + limit, "text/plain");
});
req.get_param_value() pulls a value from the query string. Use req.has_param("q") if you want to check existence first.
Read request headers
svr.Get("/me", [](const httplib::Request &req, httplib::Response &res) {
auto ua = req.get_header_value("User-Agent");
res.set_content("UA: " + ua, "text/plain");
});
To add a response header, use res.set_header("Name", "Value").
Note:
listen()is a blocking call. To run it on a different thread, wrap it instd::thread. If you need non-blocking startup, see S18. Control Startup Order withlisten_after_bind.
To use path parameters like
/users/:id, see S03. Use Path Parameters.