jgaa/restc-cpp

can I pass parameters in RestClient->Process(fun)?

bethebest0622 opened this issue · 1 comments

I read the async demo, it's very easy and elegant.

void fun(Context& ctx) {
  auto reply = ctx.Get("http://jsonplaceholder.typicode.com/posts/1");
  auto json = reply->GetBodyAsString();
}
rest_client->Process(fun);

but, can i pass parameters into the process function like this:

void fun(Context& ctx, const std::string & url) {
  auto reply = ctx.Get(url);
  auto json = reply->GetBodyAsString();
}

int main() {
  rest_client->Process(fun, "http://jsonplaceholder.typicode.com/posts/1");
}

jgaa commented

Sure. Process takes a functor, and normally that is a lambda expression. As with all lambdas, you can pass specific local variables or all local variables like in this example. the [&] statement tells the compiler to make all the local variables available as references inside Process().