Adding the Two Numbers Using C++ REST SDK

Jun 12, 2014 at 11:40 AM
I am trying this code but this is not running.. please help me.. Give me a correct code..
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <cpprest/http_listener.h>             
#include <cpprest/json.h>                      
#include <cpprest/uri.h>                     
//#include <cpprest/ws_client>                   
#include <cpprest/containerstream.h>            // Async streams backed by STL containers
#include <cpprest/interopstream.h>              // Bridges for integrating Async streams with 
#include <cpprest/rawptrstream.h>               // Async streams backed by raw pointer to 
#include <cpprest/producerconsumerstream.h>     // Async streams for producer consumer 
#include <iostream>
#include <sstream>

using namespace std;
using namespace utility;                    // Common utilities like string conversions
using namespace web;                        // Common features like URIs.
using namespace web::http;                  // Common HTTP functionality
using namespace web::http::client;          // HTTP client features
using namespace concurrency::streams;       // Asynchronous streams
using namespace web::http::experimental::listener;

int sample1()
{
    int A1, A2;
    cout << "Enter the Number1 ";
    cin >> A1;
    cout << "Enter the Number2 ";
    cin >> A2;
    int sum1 = A1 + A2;
    return sum1;
}

int main(int argc, char *args[])
{
    // Client side process
    http_client client(L"http://localhost:8080/sum1");
    client.request(methods::GET).then([](http_response response)
    {
        if (response.status_code() == status_codes::OK)
        {
            auto body = response.extract_string().get();
            wostringstream ss;
            ss << L"Server returned status code " << response.status_code() << L"." << std::endl;
            std::wcout << ss.str();
            //sample1();
            std::wcout << "SUM of Addition Result = " << body << endl;
            getchar();
        }
    });

    //server side  process
    http_listener listenerVoices_1(L"http://localhost:8080/sum1");
    listenerVoices_1.support(web::http::methods::GET, [](web::http::http_request request)
    {
        int sum1 = sample1();
        request.reply(web::http::status_codes::OK, sum1);
    }
    );
    listenerVoices_1.open().wait();
    std::string line;
    std::cout << "Press enter to exit" << std::endl;
    std::getline(std::cin, line);

    listenerVoices_1.close().wait();

    getchar();
    return 0;
}
Coordinator
Jun 12, 2014 at 5:01 PM
Hi Bharathij,

Can you please provide some more details. What do you mean by "this is not running"? Is your program crashing, hanging, not compiling, etc....? What platform are you running on?

Thanks,
Steve
Jun 13, 2014 at 5:10 AM
Hi, This program is not compiling.. i have using visual studio 2013 and then i installed the c++ rest sdk tool. . so please give me a correct source code..
Thanks,
Pari
Jun 13, 2014 at 4:07 PM
Edited Jun 13, 2014 at 4:08 PM
Probably you are missing #include "stdafx.h" line at the top of the program. It's needed for Visual Studio C++. It's not to be used with Linux. I've edited your program to make it a bit simpler. The program only work on Windows.

When submitting an issue, it's best to whittle down the program to the absolute minimum code required to demonstrate the problem. Also mention the compiler, system and error messages. We can't read your mind, you know.
// Windows listener demo
#include "stdafx.h"
#include <cpprest/http_client.h>
#include <cpprest/http_listener.h>             

using namespace std;
using namespace utility;                    // Common utilities like string conversions
using namespace web;                        // Common features like URIs.
using namespace web::http;                  // Common HTTP functionality
using namespace web::http::client;          // HTTP client features
using namespace web::http::experimental::listener;

int main(int argc, char *args[])
{
    // Be sure to allow traffic on http://localhost:8080/msg

    // Server side process
    http_listener listenerVoices_1(U("http://localhost:8080/msg"));
    std::wcout << "Program: Listening uri:" << listenerVoices_1.uri().to_string() << endl;
    listenerVoices_1.support(web::http::methods::GET, [](web::http::http_request request)
    {
        string_t msg = U("Server: uri:") + request.request_uri().to_string() + U(" Time:") + string_t(to_wstring(time(nullptr)));
        std::wcout << msg << endl;
        request.reply(web::http::status_codes::OK, msg);
    }
    );
    listenerVoices_1.open().wait();

    // Client side process
    http_client client(U("http://localhost:8080/msg"));
    client.request(methods::GET).then([](http_response response)
    {
        if (response.status_code() == status_codes::OK)
        {
            auto body = response.extract_string().get();
            std::wcout << "Client: Return:" << response.status_code() << " Body:" << body << endl;
        }
    });

    std::wcout << "Program: Press any enter to close" << endl;
    getchar();

    listenerVoices_1.close().wait();

    std::wcout << "Program: Press any enter to end" << endl;
    getchar();

    return 0;
}
Coordinator
Jun 13, 2014 at 6:50 PM
Hi Pari,

Like BSalita mentions for us to really take a look at something you need to provide more information.

Ok so you say you are using Visual Studio 2013, what OS/platform are you running on? Desktop, Windows Store? What is the compiler error you are seeing. When you say "installed cpp rest tool" I assume you are using our NuGet package?

Thanks,
Steve
Jun 14, 2014 at 5:10 AM
Edited Jun 14, 2014 at 7:37 AM
Thank You Very Much.. Lot of thanks.. steve and Bsalita..

I am using visual studio 2013 and OS(windows 7).. I installed the CPP REST SDK tool.. and then am using nuget package..
i run in CPP Console Application..

Thanks,
Pari
Jun 14, 2014 at 7:26 AM
Edited Jun 14, 2014 at 7:31 AM
Hi steve and Bsalita,
I have one more Question :

How to get a input value in command line prompt.

For Example:

Output:

Enter the Number1: 10
Enter the Number2: 10

Total value is = 20

that one is not working...
help me.. steve and Bsalita..
Jun 14, 2014 at 10:30 AM
In my rewritten example above, I had removed instances of std::cin. You can't use cin inside lamdas that are on another thread. I substituted time() to provide a somewhat unique value. If you are asking about retrieving values from the command line, use main's argc and args.
Jun 16, 2014 at 1:16 PM
Thank You.. Bsalita..


Thanks,
Pari