Modern C++ in the API World: REST, GraphQL, and gRPC in Practice

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with APIs in C++: REST, GraphQL, gRPC



When it comes to creating or consuming APIs, many developers instinctively reach for Python, Node.js, or Go. But what if you need maximum performance, low-level memory control, or you work in a legacy system where C++ reigns supreme? The answer is simple: C++ is not just alive — it thrives in the API world. Let's break down three key approaches every C++ developer should know: classic REST, modern GraphQL, and high-performance gRPC.



This article is not a dry reference manual, but a practical guide written for those who want to write real code. We'll look at how these technologies are applied in C++, provide examples with popular libraries, and help you choose the right tool for your task.



1. REST API in C++: A Classic in a Modern Implementation



REST (Representational State Transfer) remains the most common API style. It is loved for its simplicity, human-readable JSON, and versatility. In C++, we have several powerful tools for working with REST, and one of the most popular is cpprestsdk (also known as Casablanca) from Microsoft.



Writing a REST Client in C++



Imagine we need to get weather data from a public API. Here's how it looks with cpprestsdk:



#include <cpprest/http_client.h>#include <cpprest/filestream.h>#include <iostream>

using namespace web;using namespace web::http;using namespace web::http::client;

int main() { // Create an HTTP client http_client client(U("https://api.openweathermap.org/data/2.5"));

// Build a GET request with parameters uri_builder builder(U("/weather")); builder.append_query(U("q"), U("Moscow")); builder.append_query(U("appid"), U("YOUR_API_KEY")); builder.append_query(U("units"), U("metric"));

// Send the request and get the response client.request(methods::GET, builder.to_string()) .then([](http_response response) { if (response.status_code() == status_codes::OK) { return response.extract_json(); } throw std::runtime_error("Failed to fetch data"); }) .then([](json::value jsonData) { std::cout << "Temperature: " << jsonData[U("main")][U("temp")].as_double() << "°C" << std::endl; }) .wait();

return 0;}


Key points:

  • Asynchrony: cpprestsdk uses PPL (Parallel Patterns Library) and futures, which is ideal for C++.
  • Working with JSON: the built-in json::value parser makes it easy to manipulate data.
  • Cross-platform: the library works on Windows, Linux, and macOS.


When to choose REST?

REST in C++ is ideal for:

  • Simple CRUD operations
  • Integration with web services (social networks, payment systems)
  • Prototyping and low-load microservices


2. GraphQL: Query Flexibility in C++



GraphQL solves the main problem of REST — overfetching (getting unnecessary data) and underfetching (needing multiple requests to get complete information). In C++, the cppgraphql library is most commonly used for working with GraphQL, or developers write their own client based on libcurl.



Example of a GraphQL Query in C++



Suppose we need to get a user's profile and their latest posts — in REST this would require two requests, but in GraphQL it takes one:



#include <iostream>#include <curl/curl.h>#include <nlohmann/json.hpp>

using json = nlohmann::json;

static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) { ((std::string*)userp)->a

Blogs

Book Recommendations