Introduction: Why C++ for REST API Is Not Scary
Many beginner developers believe that C++ is too difficult for creating web services. However, modern C++ (starting from the C++11 standard) provides powerful and elegant tools for developing high-performance REST APIs. In this guide, we will create a full-fledged REST API in C++ in 30 minutes using the lightweight library Crow.
We will cover how to set up a server, handle GET and POST requests, work with JSON, and return data. By the end of the article, you will have a working prototype that can be used as a foundation for your projects.
What is a REST API?
REST (Representational State Transfer) is an architectural style for creating web services. A REST API uses HTTP methods (GET, POST, PUT, DELETE) to manage resources. Data is typically transmitted in JSON format.
1. Environment Setup: Installing Libraries
For a quick start, we will use the Crow library — a minimalist C++ web framework that requires no complex configuration. We will also need a library for working with JSON — nlohmann/json.
Installing Dependencies (Linux/macOS)
# Install necessary packagessudo apt-get install libboost-all-dev cmake g++
# Download Crow (single-header version)wget https://raw.githubusercontent.com/CrowCpp/Crow/master/include/crow.h
# Download the JSON librarywget https://github.com/nlohmann/json/releases/download/v3.11.2/json.hppInstalling Dependencies (Windows with MinGW)
For Windows, use vcpkg:
vcpkg install crowvcpkg install nlohmann-json2. Creating a Simple HTTP Server in C++
Let's start with a basic server that will respond to requests. Create a file main.cpp:
#include "crow.h"
int main() { crow::SimpleApp app;
// Simple GET route CROW_ROUTE(app, "/")([](){ return "Hello, world! REST API in C++ is working!"; });
// Start the server on port 8080 app.port(8080).multithreaded().run();}Compile and run:
g++ -std=c++17 -pthread main.cpp -o api_server./api_serverOpen your browser and go to http://localhost:8080. You will see a welcome message.
3. Adding JSON Handling and CRUD Operations
Now let's add JSON support and implement a simple in-memory database for storing users.
Enabling JSON Support
#include "crow.h"#include "json.hpp"#include <vector>#include <string>
using json = nlohmann::json;
struct User { int id; std::string name; std::string email;};
std::vector<User> users;int next_id = 1;GET Route for Retrieving the User List
CROW_ROUTE(app, "/users")([](){ json response = json::array(); for (const auto& user : users) { response.push_back({ {"id", user.id}, {"name", user.name}, {"email", user.email} }); } return crow::response(response.dump());});POST Route for Creating a New User
CROW_ROUTE(app, "/users").methods("POST"_method)([](const crow::request& req){ auto body = json::parse(req.body);
User new_user; new_user.id = next_id++; new_user.name = body["name"]; new_user.email = body["email"];
users.push_back(new_user);
json response = { {"id", new_user.id}, {"name", new_user.name}, {"email", new_user.email} };
return crow::response(201, response.dump());});DELETE Route for Deleting a User
Blogs
Book Recommendations
Read also