Search code examples
c++cpprest-sdk

cpprestsdk http_listener ignoring everything after #


EDIT: See my post below for answer how to fix this.

I am building a client app that will store some data in the user dropbox app folder. So currently I am using implicit grant that will redirect the user to the given redirect uri with the parameters passed after the # in the url

Example:

localhost:1666/Dropbox#access_token=...&token_type=.......

Creating a http listener over the localhost url it detects the request however everything after # is ignored and is not passed as part of the request. Is there a way to make capture the data after the #, or is there any other library that allows me to do so?

I am using the cpprestsdk https://github.com/microsoft/cpprestsdk

    web::http::experimental::listener::http_listener* l = new web::http::experimental::listener::http_listener(m_authConfig.redirect_uri());
    l->support([this](web::http::http_request request) -> void
    {
        auto uri = request.request_uri();
        auto requestPath = web::uri::split_path(uri.path());
        auto queryObjects = web::uri::split_query(uri.query());
        auto s = uri.fragment();
        if (request.request_uri().path() == U("/Dropbox")) && request.request_uri().query() != U(""))
        {
            request.reply(web::http::status_codes::OK, U("ok.") + uri.query());
        }
        else
        {
            request.reply(web::http::status_codes::OK, U("error.") + uri.query());
        }
    });
l->open().wait();

Thanks!


Solution

  • So after researching a bit, it turns out that # (fragments) are not sent back in most browsers, so to be able to get the data i return the following java-script script:

    <script> window.location.replace([location.protocol, '//', location.host, location.pathname, '?', location.hash.substring(1,location.hash.length )].join(''));</script>
    

    This will convert the hash part to a query string and redirect it the user to it so that the listener detects it.