Multiple listeners dispatch - How to handle URL variables?

May 13, 2014 at 9:42 AM
There are several examples on how to create multiple listeners to handle e.g. /users and /posts separately. Is it possible to create separate listeners for:

/users
/users/<id as int>

?

The first would handle get (retrieve list of users) and post (create new user), while the second would handle get (specific user) and put (update user). Although they have the same base URL there's not much else in common, and code wise it would be nice to organize them as separate listeners.
Coordinator
May 13, 2014 at 6:58 PM
Hi DesktopMan

Yes, we do support the above scenario. The request will be routed to the listener listening on the best matching path.
You can refer to testcase uri_routing at listener_construction_tests.cpp

Thanks
Kavya
May 13, 2014 at 7:30 PM
Hi and thanks for the reply,

The first listener would be:
http_listener listener1(web::http::uri_builder(m_uri).append_path(U("users")).to_uri());

what would the url for the second case be? Would:
http_listener listener1(web::http::uri_builder(m_uri).append_path(U("users/1")).to_uri());
match /users/2 ?

Looking at the test case you mentioned it does not test this kind of scenario, rather it uses static urls.
Coordinator
May 13, 2014 at 8:22 PM
Aah.. I misinterpreted your question.
Currently, we do not support creating multiple listeners for dynamic URL's. Workaround is to either use the same listener or design the listeners to listen on different paths: /users and /users/id/<id as int>.

Thanks
Kavya