How can I call GET request (developed using C++ REST SDK) from CGI C++?

Jul 13, 2015 at 7:42 AM
Edited Jul 13, 2015 at 10:12 AM
Hi,

Here I am calling google search REST API and displaying its result and saving this file as program.cgi.

when i run program.cgi from mangos server...it doesnt return any result.



void main()
{
map<string, string> Get;
initializeGet(Get); 
    cout << "Content-type: text/html" << endl << endl;
    cout << "<html><body>" << endl;
    cout << "</form><br /><br />" << endl;

    if ((Get.find("query") != Get.end())) {

        cout << "Given search query is  " << Get["query"] << endl;
        http_client client(U("https://ajax.googleapis.com/ajax/services/search/web"));
        auto query = uri_builder()
            .append_query(L"q", L"anything")
            .append_query(L"v", L"1.0")
            .append_query(L"rsz", 5)
            .to_string();
        client.request(methods::GET, query)
            .then([=](http_response res){
            cout << "Anything";
            //request.reply(status_codes::OK, res.extract_json().get());
        }).wait();
    }
    else {
        cout << "Search query is empty";
    }
    while (true);
    cout << "</body></html>" << endl;
}
Jul 13, 2015 at 7:30 PM
Try using the .to_string() function on your uri_builder to check if the URL is correct.

Then, use fiddler or wireshark to look at the packets being sent. It's possible that your Google APIs call is sending it something it doesn't like.

Also, why do you have a while (true); at the end of your function? That will prevent the program from finishing, which might prevent the CGI page from being sent.
Jul 15, 2015 at 7:36 AM
Hi ROschuma,

Thanks for your reply. while(true); was the issue , thanks for pointing it out.

I modified code like below and it worked :
map<string, string> Get;
initializeGet(Get); 
   cout << "Content-type: text/html" << endl << endl;
cout << "<html><body>" << endl;
cout << "</form><br /><br />" << endl;
cout << "Reaching here" << endl << endl;
   std::string ss = Get["query"];
utility::string_t s = utility::conversions::to_string_t(ss);

    cout << "Given search query is  " << endl;

    http_client client(U("https://ajax.googleapis.com/ajax/services/search/web"));
    auto query = uri_builder()
        .append_query(L"q", L"anything")
        .append_query(L"v", L"1.0")
        .append_query(L"rsz", 5)
        .to_string();

    client.request(methods::GET, query)
        .then([=](http_response res){
        ucout << res.extract_json().get();
    }).wait();
   cout << "</body></html>" << endl;
}