firstaccount firstaccount - 2 months ago 8
C++ Question

How to use different handle_post to process msg receives?(C++ REST SDK)

I create a server which is listening on port 7000. I want my server can use function

void handle_post1()
to process the first msg it receives and use
void handle_post2()
to process the second
http request
it receives.
like:

listener.support(methods::POST, handle_post1);//if this is the first msg I receive
listener.support(methods::POST, handle_post2);//if this is the second msg I receive


I tried to code like this but it doesn't work.
How could I do that?

Answer

Your listener will handle the request on the server side. With support() you don't handle a specific request but you add a handler for all subsequent requests.

In your question, you intend to handle requests differently, depending on the relative order of the request: the first should be handled by handle_post1 and the second with handle_post2. This means that you want the handling to depend on the server state.

Now it's the good time to remind that Casablanca is Microsoft's C++ REST SDK. And REST implies a stateless processing. So you will not find any support for a state-dependent handling there.

If you'd really like to pursue, you could imagine using a handle_dispatcher() that maintains some static variable that counts the requests and delegates further to handle_post1 or handle_post2 depending on the state. But this is really not advised. The goal of REST is that different requests could be dispatched to different servers, in which case your dispatcher would not produce the expected results. If you don't want to embrace this approach, you should then use a stateful framework.

Comments