Query string parameters

When handling incoming requests sometimes it is necessary to deal with query string parameter. For example, the following URL: http://example.com/location?country=canada&minrating=4&mindate=20180101 has 3 query parameters. Though one can obtain a raw query string from request header (see Request header) it is more convenient to use restinio::parse_query() function to get a key-value interface object to work with parameters:

query_string_params_t parse_query( string_view_t query_string );

Class query_string_params_t has the following interface:

class query_string_params_t
{
    // ...

    string_view_t operator [] ( string_view_t key ) const;

    optional_t< string_view_t > get_param( string_view_t key ) const noexcept;

    bool has( string_view_t key ) const noexcept;
    auto size() const noexcept;
    bool empty() const noexcept;

    auto begin() const noexcept;
    auto end() const noexcept;

    optional_t< string_view_t > tag() const noexcept;

    // ...
};

Parameters values are represented with string view. And of course one can cast parameters values with restinio::cast_to<T>().

A sample code for working with query string parameters:

restinio::request_handling_status_t handler( restinio::request_handle_t req )
{
    if( restinio::http_method_get() == req->header().method() )
    {
        std::ostringstream sout;
        sout << "GET request to '" << req->header().request_target() << "'\n";

        // Query params.
        const auto qp = restinio::parse_query( req->header().query() );

        if( 0 == qp.size() )
        {
            sout << "No query parameters.";
        }
        else
        {
            sout << "Query params ("<< qp.size() << "):\n";

            for( const auto p : qp )
            {
                sout << "'"<< p.first << "' => "<<  p.second << "'\n";
            }
        }

        if( qp.has( "debug" ) && qp[ "debug" ] == "true" )
        {
            std::cout << sout.str() << std::endl;
        }

        req->create_response()
            .append_header( restinio::http_field::server, "RESTinio query string params server" )
            .append_header_date_field()
            .append_header( restinio::http_field::content_type, "text/plain; charset=utf-8" )
            .set_body( sout.str() )
            .done();

        return restinio::request_accepted();
    }

    return restinio::request_rejected();
}

But it is more convenient to use special functions to get values from restinio key-value objects: value_or() and opt_value(), refer to Get values from RESTinio key-value containers section for details.

See also a full sample.

Additional features of parse_query

Presence of unescaped asterisk in a query string

If an incoming request is performed by some JavaScript code then the query string can contain a unescaped asterisk. For example, name=A*&location=L*.

By the default RESTinio will throw an exception if parse_query is called for such query strings. But since v.0.4.9.1 function parse_query is a template function and it can be parametrized by parse query traits type. For example, a call:

auto params = restinio::parse_query("name=A*&location=L*");

is equivalent to:

auto params = restinio::parse_query<
      restinio::parse_query_traits::restinio_defaults>("name=A*&location=L*");

A special trait javascript compatible can be used to parse query strings formed by JavaScript code:

auto params = restinio::parse_query<
      restinio::parse_query_traits::javascript_compatible>("name=A*&location=L*");

Support for “web beacon”

Since v.0.4.9 RESTinio has the support for web beacon. It means that if a query string has the form http://example.com/resource?beacon then the value of beacon will be available via query_string_params_t::tag() method.