How to grab URI segments?

Jun 26, 2014 at 8:57 PM
Hello, i found this C++ REST SDK and i think its a great add-on for the community.
This is my first post here and i hope to learn a lot here among great programmers!

i try to many things and despite reading the docs i can't find what i am looking for!

i have built a little HTTP server which i query with PHP.
i am using the latest Casablanca...
Here is my listener,
which trigger the good method in my class
    auto path = request.relative_uri().path();
    int p ="/users/"));
    printf("\n Request URI3: %s -- %d\n", path, p);
it always gives me "/" for the path ... anyways i try

i don't know how to retrieve the segments in the URI neither the params

i will have several urls to math against like:

List 10 USERS starting at index 0
  • /users/0/10
Get invoices for a user ID 123
  • /user/123/invoices/
List 10 INVOICES starting at index 0
  • /invoices/0/10
and of course i will do the same for GET-POST-PUT-DELETE

if possible i would like to get a hint so i will be able to understand :)

A big Thanks
Luc - GroupeVL
Jun 27, 2014 at 2:37 AM

What URI are you specifying while constructing the listener?

relative_uri should contain the URI path, query and fragment path of the request message. Could you also take a look at the request_uri value?
We have tests in our listener testing these scenarios and they are working fine:
Testcase uri_encoding at
Testcases at

relative_uri() returns a web::uri object. This has path(), query() and fragment() APIs that you can call to obtain the URI components.

Jun 27, 2014 at 2:01 PM
i listen on :
const http::uri uri = http::uri::encode_uri(utility::conversions::to_string_t(theUri));
new VlHttp(uri);
and the constructor VlHttp lokks like this:
VlHttp::VlHttp(const http::uri& url) : m_listener(http_listener(url))
    printf("url is: %s\n", url.to_string().c_str());,
            //.then([&m_listener](){TRACE(L"\nstarting to listen\n"); })

        while (true);
    catch (exception const & e)
        printf("%s", e.what());
        wcout << e.what() << endl;
Here is what i have
i run in the console
handle GET

 path: °þZ☻

 fragment: ÈþZ☻

 query: ░þZ☻
i guess its somekind of encoded or stream thing?
but even if i do something like:
  • utility::conversions::to_utf8string
    it does not work
i also tried to convert to c_str
  • path.c_str();
    it returns an empty string
in PHP it is super easy like
and i would run a dispatcher matching classes / methods against the fragments.
i just don't know how to implement this with casablanca
Jun 27, 2014 at 11:08 PM
The printf statement you are using is incorrect.
printf("\n Request URI3: %s -- %d\n", path, p);

printf has no option for std::string, only a C-style string. Or you could extract the c style string using c_str(). However please note relative_uri().path() returns a std::wstring (utility::string_t). you would have to convert it to std::string using utility::conversions::to_utf8string and then pass the c_str().

Alternatively, use std::wcout << path; It will print the path just fine.

Marked as answer by groupevl on 7/4/2014 at 6:44 AM
Jul 4, 2014 at 2:45 PM
Thanks to @Kavya
You got the right answer!