0

To support HTTP methods using Casablanca/cpprestsdk you can use code like this

   http_listener listener(U("http://localhost:10000/restdemo"));
 
   listener.support(methods::GET,  handle_get);
   listener.support(methods::POST, handle_post);
   listener.support(methods::PUT,  handle_put);
   listener.support(methods::DEL,  handle_del);

This works fine when handle_get, handle_post, etc. are simply functions. But as soon as I try to implement this inside a Controller class with handle_get, handle_post, etc. being methods I get errors like:

error: no matching function for call to ‘Controller::handle_get()’
error: invalid use of non-static member function ‘void Controller::handle_get(web::http::http_request)

I don't see anything in the documentation for why methods wouldn't work. I also perused through the issues and didn't see anything relating to my problem.

Is there any obvious reason why listener.support would struggle to find the methods?

cmote
  • 79
  • 1
  • 8

2 Answers2

0

I think you need to bind the methods

listener.support(methods::GET, std::bind(&Controller::handle_get, this, std::placeholders::_1));
D. Ch
  • 36
  • 3
0

http_listener::support accepts a parameter of type const std::function< void(http_request)> &handler, that means that you can pass any Callable type, so, in order to use a member function, you can use the following:

listener.support(methods::GET, [this](web::http::http_request http_request)
{
    // call your member function here or handle your request here
});

or

listener.support(methods::GET, std::bind(&MyClass::my_handler, this, std::placeholders::_1));

The first example uses a lambda with this captured, the second creates a call wrapper over function MyClasss::my_handler