HTTP requests in C++

Asked

Viewed 1,442 times

8

How could 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 it in my project, accessing json as obj.status for example.

  • 1

    When trying to answer the question, I realized that "making an HTTP request" and "interpreting a JSON" are two sufficiently distinct steps that each deserved a separate question. Anyway, I left my answer.

2 answers

10


According to that question on Soen, how to make an HTTP request depends on the operating system and/or the use of external libraries. According to the accepted answer, the recommended form is through the library libcurl, using one of his bindings for C++. That answer has an example of code 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 it is initially possible to use libcurl in Windows as well, there is an alternative solution that uses only the native libraries of this, in that other answer.

Once the HTTP request has been made and the response has been obtained, simply interpret the JSON. The page json.org lists several library options to do the Parsing. It’s hard to suggest one, because there are so many, and even in Soen* there is 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 on 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 on Soen)

  • Your answer is better than any I had met at SOEN. I followed your suggestion and tested the Rapidjson, maybe it is still early to say, but at first it is working very well for me. PS: your reply link to SOEN is broken.

  • @Math as happens a lot with some users here.

2

You are looking to integrate a code system into different languages.

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

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

At the time of college, back in 2004, I studied this and did some integrations of Java with C; then I ended up not working with it anymore so I don’t know if there is something more current.

I saw that there is the extension for php here: http://sourceforge.net/projects/phporb/

And the extension to C++ here: http://omniorb.sourceforge.net/

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

  • 1

    From what I understood of the question, what the author wants is to open a URL from its C++ code, which happens to be in PHP, but could use any technology. That is, it is a matter of making an HTTP request to a specific server, not integrating two languages (in which case your answer would be excellent).

  • Yes, I do not need to integrate two languages but to create a communication route between two different applications.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.