monitoring page

Feb 26, 2015 at 4:10 PM
Edited Feb 27, 2015 at 8:22 AM
I want to create a page (html) that is displaying information about the state of my application, like display running in page until the application ends, and if I connect to the page, I'll see the app's status. I have started from a similar application, but it is getting more and more complicated. Is there a tutorial for such a thing? I want to refresh the page every some time.

Until now I have managed to do as this:
#pragma once

#include <functional>

#include <cpprest/basic_types.h>
#include <cpprest/http_msg.h>
#include <cpprest/http_listener.h>

class Server
    void handle_put(web::http::http_request message);
    void handle_post(web::http::http_request message);
    void handle_delete(web::http::http_request message);

    web::http::experimental::listener::http_listener m_listener;

    Server(utility::string_t url, std::function< void(web::http::http_request) > handler);

    pplx::task<void> open();
    pplx::task<void> close();
and in main I do:
utility::string_t address = U("http://" + server_addr + ":");


// my appli run

There is a global std::unique_ptr<Server> gServer that is called in the two functions:
void onInit(const utility::string_t& address)
    web::uri_builder uri(address);

////// marked
    std::function<void(web::http::http_request)> har_func = std::bind(&server::request_handler::handle_a_request, request_handler, std::placeholders::_1);
    gServer = std::unique_ptr<Server>(new Server(uri.to_uri().to_string(), har_func));

void onExit()
The marked is getting complicated. In fact there are more handle_a_request functions that are creating the tables that I have spoken in the beginning, and I am getting lost here... Maybe I don't even need that part.

P.S.: the server::request_handler is defined in the application (that I have inspired from), is there another simpler way to do it, like some implicit thing from cpprest?
Feb 26, 2015 at 5:46 PM
Hi opreanstefan,

It is a little hard to follow the code here and understand exactly what you are asking for. I don't see the 'Server' class declared anywhere, but the http_listener is stored in a class called 'Monitoring'. What exactly is the purpose of this 'handle_a_request', is it to try and handle just a single HTTP request and not subsequent ones? I don't see why you need to have more request handlers.

In general the way you register handlers with our http_listener for requests is through the http_listener::support(...) APIs. There is one that can be used to handle all request, regardless of HTTP method, and another overload that can be used to register for specific HTTP methods.

Feb 27, 2015 at 8:15 AM
Edited Feb 27, 2015 at 8:45 AM
I said that I have started to inspire from another application that has something like that. "Monitoring" I have renamed, it was called "Server". But anyway, the idea is that I need a page to verify the state of my application. I want to be able to verify if the application is running or not, and I have thought that if I create a page (or server) at the start of the application that has message (like a json: {"state":"on"}), and like this if someone is accessing the http://x.x.x.x:9000 (for example), it can see if the application is still running or not. I would appreciate your support in this, even if it totally different from the example I have started.

I am on Linux, so I have seen a http_linux_server class, maybe with that, it will be easier?
Feb 27, 2015 at 5:28 PM
Hi opreanstefan,

Ok based on what you've describe then it sounds like you need to create an http_listener for processing requests querying about the status of your other application. Then in your http_listener you need to have a way to determine if your application is up and running and respond to the HTTP requests appropriately.

Don't take a look at or use http_linux_server. It shouldn't be part of the reference documentation and is technically an implementation detail that needs to be under a details namespace. The http_listener is the correct class.

Mar 3, 2015 at 12:41 PM
Edited Mar 3, 2015 at 2:11 PM
I did not arrive to make it work, so I have started a simple thing aside: just a listener
#include <cpprest/http_listener.h>
#include <functional>

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

void handle_get(http_request message)
    message.reply(status_codes::OK, U("Hello, World!"));

void handle_post(http_request message)

void handle_put(http_request message)

void handle_delete(http_request message)

#define TRACE(msg)            std::wcout << msg
#define TRACE_ACTION(a, k, v) std::wcout << a << L" (" << k << L", " << v << L")\n"

int main(int argc, char ** argv)
  uri_builder uri(U(""));
  http_listener listener(uri.to_uri());, handle_get);, handle_post);, handle_put);, handle_delete);

        .then([&listener](){TRACE(L"\nstarting to listen\n");})

      while (true);
  catch (std::exception const & e)
     std::wcout << e.what() << std::endl;
  catch (...)
     std::wcout << "Unknown exception" << std::endl;

  return 0;
but it seems that it does not work nigher. When I try to access the page (I have also tried or 2000 or many others) I get a No data received, Unable to load the webpage because the server sent no data. Error code: ERR_EMPTY_RESPONSE. What am I doing wrong? And what is more, is that the application crashes with no exception...

If I run step by step, I have the impression that first the application crashes and then the "No data received" page is returned (and sometimes I also receive "This webpage is not available")...
Mar 3, 2015 at 5:57 PM
Hi opreanstefan,

You could try using 'localhost' for the hostname component of the listener URI. Can you try debugging your listener application and see if it is actually receiving a request in the support handlers? Also you mention something about a crash, where exactly does it crash and what is reported?

Mar 4, 2015 at 8:26 AM
I have tried to use localhost instead of http://x.x.x.x:port, and now it is not crashing anymore., but still "This webpage is not available" and "Error code: ERR_CONNECTION_REFUSED"...

If I do debug, it goes till the while loop and stays there, even if I try to access localhost...

On the other hand, if I add a port: localhost:61561, it crashes when I try to access the page. I get a "No data received" and "Unable to load the webpage because the server sent no data. Error code: ERR_EMPTY_RESPONSE"

If I debug it, It crashes in the while loop, with no passing through one of the handles and no exception is caught... I suppose that there is some other thing to be done, like an enabling something, I work on Linux Ubuntu, shall I set a flag or something?