Skip to content

A C++ wrapper library that provides a convenient and secure way to make HTTP API requests, while handling JSON data. Built on top of libcurl for robust networking capabilities and nlohmann/json for JSON data processing.

License

Notifications You must be signed in to change notification settings

mmaarij/Secure-LibCurl-Json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Secure LibCurl Wrapper in C++ for HTTP(S) API Calls with Nlohmann Json Support

This project uses 2 simple files to create the wrapper with SecureLibCurlJson.h containing the definitions and SecureLibCurlJson.cpp containing all the implementaions.

This repository has 2 branches:

  1. Windows
  2. Linux

Table of Contents


Windows (Visual Studio 2022)

The entire Visual Studio 2022 Project is contained in the Windows Branch

Project Settings for Libcurl

Note : These steps have already been performed in the visual studio project included in this repo, but, they are documented if you need to setup a new project.

  • Download Curl for Windows
  • Extract:
    • The include and lib folders and keep them in a new folder called [your-curl-dir] (it is named curl-8.4.0_3-win64-mingw in this repo's case)
    • The bin/libcurl-x64.dll file inside and keep it in your project directory.
  • Download CA certificate extracted from Mozilla cacert.pem and place it inside your project directory.
  • Add {your-curl-dir}/include to Configuration Properties -> VC++ Directories -> Include Directories.
  • Add {your-curl-dir}/lib to Configuration Properties -> VC++ Directories -> Library Directories.
  • In Configuration Properties -> Linker -> Input -> Additional Dependencies, add these followings lines:
{your-curl-dir}/lib/libcurl_a.lib
Ws2_32.lib
Wldap32.lib
Crypt32.lib
Normaliz.lib
  • In Configuration Properties -> Build Events -> Post-Build Event -> Command Line add the following lines to copy necessary files when the .exe is generated:
copy "libcurl-x64.dll" "$(SolutionDir)$(Platform)\$(Configuration)\libcurl-x64.dll"
copy "cacert.pem" "$(SolutionDir)$(Platform)\$(Configuration)\cacert.pem"

Project Settings for Nlohmann Json

  • Download json.hpp from Nlohmann Json and keep it in a folder called nlohmann in your project directory
  • You can now use the library as follows:
#include "nlohmann/json.hpp"

using json = nlohmann::json;

Directory Structure

This is what the directory structure should look like after setting up

.
├── curl-8.4.0_3-win64-mingw/
│   ├── include/
│   │   └── [all the files inside include as-is]
│   └── lib/
│       └── [all the files inside lib as-is]
├── nlohmann/
│   └── json.hpp
├── cacert.pem
├── libcurl-x64.dll
├── SecureLibCurlJson.h
├── SecureLibCurlJson.cpp
└── Source.cpp

Linux (Tested on Ubuntu 22.04 LTS)

The Linux code is contained in the Linux Branch

Install Prerequisites (Libcurl and Nlohmann JSON)

Run the following commands in the terminal

sudo apt update
sudo apt upgrade
sudo apt install libcurl4-openssl-dev
sudo apt install nlohmann-json3-dev

Run the following command in the terminal from inside the directory with the source code

curl --etag-compare etag.txt --etag-save etag.txt --remote-name https://curl.se/ca/cacert.pem

Directory Structure

This is what the directory structure should look like

.
├── cacert.pem
├── etag.txt
├── SecureLibCurlJson.h
├── SecureLibCurlJson.cpp
└── Source.cpp

Compile

g++ -o SLCJ.out Source.cpp SecureLibCurlJson.cpp -lcurl

Run the program

./SLCJ.out

Usage

Example usage is provided in Source.cpp

  SecureLibCurlJson curlLib(true);

  std::string apiUrl = "https://api.publicapis.org/entries";
  std::string requestType = "GET";

  std::map<std::string, std::string> queryParameters;
  /*  EXAMPLE USAGE OF QUERY PARAMETERS
  *   queryParameters["key"] = "abcxyz123";
  *   queryParameters["query"] = "helloworld";
  */

  json bodyParameters;
  /*  EXAMPLE USAGE OF BODY PARAMETERS
  *   bodyParameters["value1"] = "one";
  *   bodyParameters["value2"] = "two";
  *   -- if json objects need to be nested --
  *   json nestedValues;
  *   nestedValues["nestedValue1"] = "n_one";
  *   nestedValues["nestedValue2"] = "n_two";
  *   bodyParameters["nested"] = nestedValues;
  */

  json responseData = curlLib.MakeApiRequest(apiUrl, requestType, queryParameters, bodyParameters);

  if (!responseData.empty())
  {
      std::cout << "Response Data: " << responseData.dump(2) << std::endl;
  }
  else
  {
      std::cerr << "API request failed." << std::endl;
  }

About

A C++ wrapper library that provides a convenient and secure way to make HTTP API requests, while handling JSON data. Built on top of libcurl for robust networking capabilities and nlohmann/json for JSON data processing.

Topics

Resources

License

Stars

Watchers

Forks