How to grab URI segments?

Jun 26, 2014 at 7: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
m_listener.support(methods::GET,
        std::tr1::bind(&VlHttp::handle_get,
        this,
        std::tr1::placeholders::_1));
which trigger the good method in my class
    auto path = request.relative_uri().path();
    int p = path.compare(U("/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
Coordinator
Jun 27, 2014 at 1:37 AM
Hi

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 https://casablanca.codeplex.com/SourceControl/latest#Release/tests/Functional/http/listener/requests_tests.cpp
Testcases at https://casablanca.codeplex.com/SourceControl/latest#Release/tests/Functional/http/listener/request_relative_uri_tests.cpp

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

Thanks
Kavya
Jun 27, 2014 at 1:01 PM
i listen on :
with:
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());
    m_listener.support(methods::GET,
        std::tr1::bind(&VlHttp::handle_get,
        this,
        std::tr1::placeholders::_1));
    m_listener.support(methods::PUT,
        std::tr1::bind(&VlHttp::handle_put,
        this,
        std::tr1::placeholders::_1));
    m_listener.support(methods::POST,
        std::tr1::bind(&VlHttp::handle_post,
        this,
        std::tr1::placeholders::_1));
    m_listener.support(methods::DEL,
        std::tr1::bind(&VlHttp::handle_delete,
        this,
        std::tr1::placeholders::_1));
    try
    {
        m_listener
            .open()
            //.then([&m_listener](){TRACE(L"\nstarting to listen\n"); })
            .wait();

        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
$_SERVER['REQUEST_URI'];
and i would run a dispatcher matching classes / methods against the fragments.
i just don't know how to implement this with casablanca
Coordinator
Jun 27, 2014 at 10: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.

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