{"id":106803,"date":"2022-06-29T07:00:00","date_gmt":"2022-06-29T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=106803"},"modified":"2022-06-29T07:27:13","modified_gmt":"2022-06-29T14:27:13","slug":"20220629-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20220629-00\/?p=106803","title":{"rendered":"How can I parse URL query string in the Windows Runtime?"},"content":{"rendered":"<p>In C#, you can use the <code>HttpUtility.<wbr \/>ParseQueryString<\/code> method to parse URL query strings. For C++\/WinRT, you can use the Windows Runtime <code>Windows.<wbr \/>Foundation.<wbr \/>WwwFormUrlDecoder<\/code> class.<\/p>\n<pre>Uri uri{ L\"https:\/\/www.youtube.com\/watch?v=%64Qw4w9WgXcQ&amp;t=43s\" };\r\nauto decoder = uri.QueryParsed();\r\n\r\n\/\/ or you can parse a query string directly\r\nWwwFormUrlDecoder decoder(L\"v=%64Qw4w9WgXcQ&amp;t=43s\");\r\n<\/pre>\n<p>Once it&#8217;s parsed, you can pull out the pieces.<\/p>\n<pre>auto id = decoder.GetFirstValueByName(L\"v\");\r\n<\/pre>\n<p>The <code>Www\u00adForm\u00adUrl\u00adDecoder<\/code> is a bit difficult to work with, however, because the <code>GetFirstValueByName<\/code> method throws an exception if the key is not present at all. Since you are usually parsing query strings from potentially-untrusted sources, you can&#8217;t be certain that the value you want is present.<\/p>\n<p>What you can do is convert the parsed query into some other format that is easier to work with, say, a <code>std::multimap<\/code>:<\/p>\n<pre>auto to_multimap(\r\n    winrt::Windows::Foundation::WwwFormUrlDecoder const&amp; decoder)\r\n{\r\n    std::multimap&lt;winrt::hstring, winrt::hstring&gt; parsed;\r\n    for (auto&amp;&amp; entry : decoder) {\r\n        parsed.emplace(entry.Name(), entry.Value());\r\n    }\r\n    return parsed;\r\n}\r\n<\/pre>\n<p>Now you can pull out the values from the query in a way you are more comfortable with. And you can even use the usual multimap methods to extract multiple-valued keys, if that&#8217;s something you care about.<\/p>\n<pre>auto parsed = to_multimap(uri.QueryParsed());\r\n\r\n\/\/ Does a \"v=\" key exist?\r\nif (!parsed.contains(L\"v\"))\r\n{\r\n    error();\r\n}\r\n\r\n\/\/ Process all the \"v=\" keys.\r\nauto values = parsed.equal_range(L\"v\");\r\nfor (auto it = values.first; it != values.second; ++it)\r\n{\r\n    process_value(it-&gt;second);\r\n}\r\n\r\n\/\/ Process the first \"v=\" key, if any.\r\nauto it = parsed.lower_bound(L\"v\");\r\nif (it != parsed.end() &amp;&amp; it-&gt;first == L\"v\") {\r\n    process_value(it-&gt;second);\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>There&#8217;s a query parser built in, but it&#8217;s a little cantankerous.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-106803","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>There&#8217;s a query parser built in, but it&#8217;s a little cantankerous.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/106803","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=106803"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/106803\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=106803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=106803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=106803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}