jgaa / restc-cpp

Modern C++ REST Client library
MIT License
617 stars 95 forks source link

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

Open bethebest0622 opened 1 year ago

bethebest0622 commented 1 year ago

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 1 year ago

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().