HTTP requests in C ++

8

How can I make a request in a URL that would return a jSON in C ++?

I need to access a /return.php URL that returns the string

{"status":true,"hash":"12#87!!3@WSS\/.","user":"admin"}

and work on my project, accessing json as obj.status for example.

    
asked by anonymous 05.09.2014 / 16:34

2 answers

10

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 )

    
07.09.2014 / 22:03
2

You are wanting to integrate a system made up of codes into different languages.

One option for integration of legacy systems is the Common Object Request Broker Architecture (CORBA).

It is basically a standard for intercom. The various languages study the norm and create libraries for this extension. In this case the extension of "communication via CORBA".

At college time, back in 2004, I studied this and did some Java integrations with C; then I did not work with it anymore so I do not know if there is anything more current.

I saw the extension for php here: link

And the extension to C ++ here: link

I do not know if they are the best libraries for this because I have not used or tested it, but I believe that this is the path you should follow.

    
07.09.2014 / 15:36