JSON API » Sample clients » C++

// This is a sample client for the Enswitch JSON API.
// It is released into the public domain.
// It comes with absolutely no warranty, and is not supported.

// Compile with: g++ `pkg-config --cflags jsoncpp` `pkg-config --cflags curlpp` sample.cpp `pkg-config --libs jsoncpp` `pkg-config --libs curlpp` -o sample

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
#include <jsoncpp/json/json.h>

using namespace std;

int main( int argc, char **argv ) {
    std::string base = "http://enswitch.example.com/api/json/";
    std::string function = "access/list";
    std::string username = "testuser";
    std::string password = "testpass";

    std::ostringstream os;
    curlpp::Easy c;
    Json::Value root;
    Json::Reader reader;

    c.setOpt( new curlpp::options::Url( base + function + "?auth_username=" + username + ";auth_password=" + password ) );
    c.setOpt( new curlpp::options::FollowLocation( "true" ) );
    os << c;

    reader.parse( os.str(), root, false );
    const Json::Value data = root[ "data" ];
    for ( unsigned int index = 0; index < data.size(); ++index ) {
        const Json::Value v = data[ index ];
        cout << "Remote access account id=" << v[ "id" ].asString() << ", name=" << v[ "name" ].asString() << endl;
    }

    return 0;
}