Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bethebest0622 opened this issue Jun 19, 2023 · 1 comment
Open

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

bethebest0622 opened this issue Jun 19, 2023 · 1 comment
Labels

Comments

@bethebest0622
Copy link

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
Copy link
Owner

jgaa commented Jun 23, 2023

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

@jgaa jgaa added the question label Jun 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants