Hello World with C++ REST SDK

Feb 8, 2014 at 2:09 PM
Edited Feb 8, 2014 at 2:10 PM
Hello, I am trying make a hello world program wich prints in a web browser the message "Hello World" but I'm not understand how to do this. Someone can help me?

I'm try make a server, when I open the URL in web browser, the server will respond with the message. The code I used was for the server is:
#include "stdafx.h"
#include <cpprest\http_client.h>
#include<cpprest\http_listener.h>
#include <iostream>

using namespace web::http::experimental::listener;
using namespace web::http;
using namespace web;

int main(int argc, char* argv[]){
    http_listener listener(U("http://localhost:1234/"));
    listener.open().wait();
    listener.support(methods::GET, [](http_request request){
        request.reply(http::status_codes::OK, U("Hello World!"));
    });
    listener.close();
    return 0;
}
Coordinator
Feb 9, 2014 at 11:10 PM
Hi Ichibection

In the above code, I see that you are opening the listener, attaching a handler to handle GET requests, and then closing the listener. The process will exit immediately after starting. When a browser(client) sends any request, the listener would have already exited. I would suggest waiting for some indication from the user to exit the listener or have the listener listening for certain time interval and then call close. For instance, please see below:
    std::string line;
    std::wcout << U("Hit Enter to close the listener.");
    std::getline(std::cin, line);

    listener.close().wait();
Also, one more suggestion is to add the request handler (.support()) before calling listener.open(). This way, when you open the listener, the listener is ready to respond to requests.

Thanks
Kavya
Feb 10, 2014 at 2:15 AM
Edited Feb 10, 2014 at 2:18 AM
Thank you kavyako, I did the "Hello World" works. But now, I'm trying make a server of a aplicattion in C++ (C plus plus) wich the output data I want to show in the browser. I store the data in a list<string> but I can not send the string is in the variable to the browser. If a use -> U("test") <- works, but from a string variable don't works. Can you help me?
list<string> lista = tarefa5();
value answer;
int i = 0;
for (list<string>::iterator it = lista.begin(); it != lista.end(); ++it)
{
    string x = *it;
    utility::string_t s = utility::conversions::utf8_to_utf16(x);
    value v = value::string(s);
    answer[i] = v;
    wcout << answer[i] << endl;
    i++; // i plus plus
}
request.reply(status_codes::OK, answer);
In the server console, the strings appear.
Coordinator
Feb 10, 2014 at 8:03 PM
If you call request.reply() with json data (answer), the response is sent with content-type = application/json. The browser may or may not display the JSON data as a string, it depends on how the browser handles content-type application/json.
To send the JSON value as a string (content-type = text/plain), I would suggest passing answer.to_string() in the response.

Thanks
Kavya.