This is a c++ library originally written for accessing GitHub REST API v3. Currently reorganized to be easily used with any Rest API available.
It supports three backends for establishing connections with remote API servers: Qt5/6, Curl and cpp-httplib.
This repository has submodules which are not necessary to build and use this project.
You may need them if you want to build unit tests (CppRestAPI_Tests
cmake varible set to TRUE
).
Another submodule is vcpkg
which can simplify build by providing required dependencies.
Please mind that vcpkg uses telemetry.
Visit https://learn.microsoft.com/vcpkg/about/privacy for more details:
This is a CMake based project and is meant to be included as a subproject.
Simply embed cpp_restapi's sources in your project,
choose which http backend you prefer (all can be used simoultanously) and include cpp_restapi
project in your CMakeLists.txt
like this:
set(CppRestAPI_QtBackend ON) # use this line if you prefer Qt backend
set(CppRestAPI_CurlBackend ON) # use this line if you prefer Curl backend
set(CppRestAPI_CppHttplibBackend ON) # use this line if you prefer cpp-httplib backend
add_subdirectory(cpp_restapi)
Then you can link your application against cpp_restapi:
target_link_libraries(app
PRIVATE
cpp_restapi
)
and that's all.
Depending on your choice of backend you may need to install libcurl, Qt and/or cpp-httplib libraries.
Qt backend can be compiled with Qt5 (default) or Qt6.
Set CppRestAPI_UseQt6
CMake variable to TRUE
to use Qt6.
#include <iostream>
#include <cpp_restapi/curl_connection.hpp>
int main(int argc, char** argv)
{
// Access The Star Wars API
cpp_restapi::CurlBackend::Connection connection("https://swapi.dev/api", {});
std::cout << connection.get("people/1") << '\n';
std::cout << connection.get("starships/12/") << '\n';
return 0;
}
This example accesses The Star Wars API using curl backend.
As you can see it is enought to instantiate cpp_restapi::CurlBackend::Connection
object providing API url and after that request can be made.
Qt version:
#include <iostream>
#include <QCoreApplication>
#include <QNetworkAccessManager>
#include <cpp_restapi/curl_connection.hpp>
int main(int argc, char** argv)
{
QCoreApplication qapp(argc, argv);
QNetworkAccessManager manager;
// Access The Star Wars API
cpp_restapi::QtBackend::Connection connection(manager, "https://swapi.dev/api", {});
std::cout << connection.get("people/1") << '\n';
std::cout << connection.get("starships/12/") << '\n';
return 0;
}
cpp-httplib version:
#include <iostream>
#include <cpp_restapi/cpp-httplib_connection.hpp>
int main(int argc, char** argv)
{
// Access The Star Wars API
cpp_restapi::CppHttplibBackend::Connection connection("https://swapi.dev/api", {});
std::cout << connection.get("people/1") << '\n';
std::cout << connection.get("starships/12/") << '\n';
return 0;
}
For accessing GitHub API it is possible to use exactly the same apporach as presented above.
However, for conveniance, there are also additional helpers available:
#include <QCoreApplication>
#include <QDebug>
#include <QNetworkAccessManager>
#include <cpp_restapi/qt_connection.hpp>
#include <cpp_restapi/github/connection_builder.hpp>
#include <cpp_restapi/github/request.hpp>
int main(int argc, char** argv)
{
QCoreApplication qapp(argc, argv);
QNetworkAccessManager manager;
auto connection = cpp_restapi::GitHub::ConnectionBuilder().build<cpp_restapi::QtBackend::Connection>(manager);
cpp_restapi::GitHub::Request request(connection);
qInfo() << request.getRateLimit().c_str();
qInfo() << request.getUserInfo("Kicer86").c_str();
return 0;
}
Here connection is being build with ConnectionBuilder
.
Builder provides methods for setting additional connection parameters (passed as a second argument to Connection
after API url).
It also sets the API url automatically.
Refer documentation of ConnectionBuilder
for more details.
Additionaly there is also cpp_restapi::GitHub::Request
class available which comes with accessors to most common API requests.
#include <iostream>
#include <cpp_restapi/curl_connection.hpp>
#include <cpp_restapi/github/connection_builder.hpp>
#include <cpp_restapi/github/request.hpp>
int main(int argc, char** argv)
{
auto connection = cpp_restapi::GitHub::ConnectionBuilder().build<cpp_restapi::CurlBackend::Connection>();
cpp_restapi::GitHub::Request request(connection);
std::cout << request.getRateLimit() << '\n';
std::cout << request.getUserInfo("Kicer86") << '\n';
return 0;
}
#include <iostream>
#include <cpp_restapi/cpp-httplib_connection.hpp>
#include <cpp_restapi/github/connection_builder.hpp>
#include <cpp_restapi/github/request.hpp>
int main(int argc, char** argv)
{
auto connection = cpp_restapi::GitHub::ConnectionBuilder().build<cpp_restapi::CppHttplibBackend::Connection>();
cpp_restapi::GitHub::Request request(connection);
std::cout << request.getRateLimit() << '\n';
std::cout << request.getUserInfo("Kicer86") << '\n';
return 0;
}
Also please look into 'examples' directory for details.
Code documentation available at https://kicer86.github.io/cpp_restapi/index.html