According to this question in SOen , how to make an HTTP request depends on the operating system and / or the use of external libraries. According to the accepted response, the recommended way is through the libcurl library, using one of your bindings to C ++ . This response has a code sample using the second link:
// Edit : rewritten for cURLpp 0.7.3
// Note : namespace changed, was cURLpp in 0.7.2 ...
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
// RAII cleanup
curlpp::Cleanup myCleanup;
// standard request object.
curlpp::Easy myRequest;
// Set the URL.
myRequest.setOpt(new curlpp::options::Url(std::string("http://www.wikipedia.com")));
// Send request and get a result.
// By default the result goes to standard output.
// Here I use a shortcut to get it in a string stream ...
std::ostringstream os;
os << myRequest.perform();
string asAskedInQuestion = os.str();
Although at first it is possible to use libcurl in Windows as well, there is a workaround that only uses libcurl's native libraries, in that other answer .
Once the HTTP request is obtained and the response is obtained, it is enough to interpret the JSON. The json.org page lists several library options for parsing . It's hard to suggest one because there are so many, and same in SOen * there is not a good answer with examples, but I found the RapidJSON at first glance quite simple to use:
// rapidjson/example/simpledom/simpledom.cpp'
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>
using namespace rapidjson;
int main() {
// 1. Parse a JSON string into DOM.
const char* json = "{\"project\":\"rapidjson\",\"stars\":10}";
Document d;
d.Parse(json);
// 2. Modify it by DOM.
Value& s = d["stars"];
s.SetInt(s.GetInt() + 1);
// 3. Stringify the DOM
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
d.Accept(writer);
// Output {"project":"rapidjson","stars":11}
std::cout << buffer.GetString() << std::endl;
return 0;
}
* Note: broken link (the question in SOen, "What's the best C ++ JSON parser?") has been removed and can only be accessed by anyone who has 10,000 reputation points or more in SOen )