Boost::Beast minimal HTTP server example using boost::json

The following minimal example shows how to use Boost::Beast to create a simple HTTP server that returns a JSON response.

Note: This example does not include request routing. For an extended example including request routing, see Boost::Beast minimal HTTP server example with request routing using Boost::URL and JSON responses.

#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/asio.hpp>
#include <boost/json.hpp>
#include <iostream>

namespace beast = boost::beast;     // from <boost/beast.hpp>
namespace http = beast::http;      // from <boost/beast/http.hpp>
namespace net = boost::asio;       // from <boost/asio.hpp>
namespace json = boost::json;      // from <boost/json.hpp>
using tcp = boost::asio::ip::tcp;  // from <boost/asio/ip/tcp.hpp>

// Function to handle an HTTP request and generate a JSON response
void handle_request(const http::request<http::string_body>& req, http::response<http::string_body>& res) {
    if (req.method() == http::verb::get) {
        json::object json_response;
        json_response["message"] = "Hello, World!";
        json_response["status"] = "success";

        res.result(http::status::ok);
        res.set(http::field::content_type, "application/json");
        res.body() = json::serialize(json_response);
        res.prepare_payload();
    } else {
        res.result(http::status::method_not_allowed);
        res.set(http::field::content_type, "text/plain");
        res.body() = "Method Not Allowed";
        res.prepare_payload();
    }
}

// Session to handle communication with a single client
void session(tcp::socket socket) {
    try {
        beast::flat_buffer buffer;

        // Read an HTTP request
        http::request<http::string_body> req;
        http::read(socket, buffer, req);

        // Prepare the response
        http::response<http::string_body> res;
        handle_request(req, res);

        // Write the response
        http::write(socket, res);
    } catch (const std::exception& e) {
        std::cerr << "Error in session: " << e.what() << '\n';
    }
}

// Main function to set up the server
int main() {
    try {
        const auto address = net::ip::make_address("127.0.0.1");
        const unsigned short port = 8080;

        net::io_context ioc;

        // Create and bind the acceptor
        tcp::acceptor acceptor{ioc, {address, port}};
        std::cout << "HTTP server is running on http://127.0.0.1:8080\n";

        while (true) {
            // Accept a new connection
            tcp::socket socket{ioc};
            acceptor.accept(socket);

            // Handle the session in a new thread
            std::thread{&session, std::move(socket)}.detach();
        }
    } catch (const std::exception& e) {
        std::cerr << "Error: " << e.what() << '\n';
        return 1;
    }
}

How to compile

g++ -std=c++17 -O2 httpserver.cpp -o server -lboost_system -lboost_thread -lboost_json -lpthread

Output

Open your browser and navigate to http://127.0.0.1:8080.

You should see the following JSON response:

{"message":"Hello, World!","status":"success"}