6

I have a .NET Core 6.0 Azure Function HTTP Trigger.

I would it like to handle an array query string parameter in both of these common forms:

  • ?param=foo&param=escape%2Ctest
  • ?param=foo,escape%2Ctest

In both cases I would like to extract the array ["foo", "escape,test"].

I was surprised to find that the query string parse options available to me either did not support the comma syntax or URL decoded the query string as part of grouping the parameters.

I've tried using several different dotnet APIs:

Here's a demo in .NET Fiddle:

var qs = "?param=foo,escape%2Ctest";

var a = httpRequest.Query["param"];
// ["foo,escape,test"]

var b = HttpUtility.ParseQueryString(qs)["param"];
// "foo,escape,test"

var c = QueryHelpers.ParseQuery(qs)["param"];
// ["foo,escape,test"]

I am a little surprised that there is no support for the comma separated format. And I am very surprised that there does not seem to be a way to get the segmented query string without also decoding the query string parameters (which prevents distinguishing between param=foo,escape%2Ctest and ?param=foo,escape,test).

I will post my current solution as an "answer" below, but I am hoping that I am missing a simpler solution that uses a .NET library. If anyone can point out what I've missed here, I would appreciate it.

KyleMit
  • 30,350
  • 66
  • 462
  • 664
Zach
  • 316
  • 4
  • 14
  • Unfortunately the RFCs have always been vague about how the "query" part should or could be interpreted and different conventions have arisen over the years. Your desired behavior, and the way of distinguishing between escaped commas in particular, is to my knowledge not explicitly standardized. `?param=foo&param=escape%2Ctest` has the benefit of being unambiguous as to the separation of values (and is handled correctly by `QueryHelpers`), `?param=foo,escape%2Ctest` is at least dubious, and should probably not be produced by "sane" implementations. – Jeroen Mostert Feb 21 '22 at 21:47

2 Answers2

3

Here is my current solution. Again, I'm hoping there is a less manual way to achieve this behavior.

public static IDictionary<string, List<string>> GroupByKey(this QueryString queryString)
{
  if (!queryString.Contains("?"))
  {
    throw new ArgumentException("Expected query string to contain '?' character.");
  }

  var encodedUrl = queryString.Value;
  var result = new Dictionary<string, List<string>>();
  string trimmedQueryString = encodedUrl.Split('?')[1];
  string[] queryStringSections = trimmedQueryString.Split('&');
  foreach (var section in queryStringSections)
  {
    var sides = section.Split('=');
    var key = sides[0];
    var value = sides[1];
    var values = value.Split(',');
    var decodedValues = values.Select(x => WebUtility.UrlDecode(x));

    if (!result.ContainsKey(key))
    {
      result.Add(key, new List<string>());
    }

    result[key].AddRange(decodedValues);
  }

  return result;
}
Zach
  • 316
  • 4
  • 14
  • 1
    This assumes that there will always be a "?" and something to the left of it. Is that guaranteed to be the case? In my experience, it isn't. You're making the same assumption about the "=". Unless you have total control over these URLs, these assumptions make me really nervous. – Mike Hofer Feb 21 '22 at 21:34
  • Thanks @MikeHofer. I edited it to throw an exception if the "?" is not present. (note: Split with nothing left of the "?" will still produce a empty "left" element in the array). I am aware this still isn't a complete parser. Like, it doesn't fail intelligently if there are two "?" in the query string. That's why I was hoping for a more pre-baked solution. – Zach Feb 21 '22 at 21:43
  • You may not find one. It's a pretty specific use case. In cases like these, writing your own is perfectly acceptable. So get it working, then refactor it to make it maintainable, and then document the crap out of it. – Mike Hofer Feb 21 '22 at 21:48
3

Unfortunately, there is no official specification in RFC 3986 > Section 3.4 detailing how to pass multiple values for a query parameter.

That said, browsers do seem to natively implement the multi-key approach when building a query string on their own. For example, in the following demo:

<form method="GET">
  <label for="cars">Choose a car:</label>
  <select name="cars" id="cars" multiple>
    <option value="audi" selected>Audi</option>
    <option value="saab" selected>Saab</option>
  </select>
  <button type="submit">Submit</button>
</form>

The get request will be sent with ?cars=audi&cars=saab

In the absence of a spec, with dotnet already supporting the multi-key approach with your desired result, and since browsers use multi-key natively, it may be worth updating the way you build query strings on the client.

Perhaps someone else can add a some clever way to parse in .net, but otherwise you may be fighting uphill against the implementation in dotnet.

Further Reading:

KyleMit
  • 30,350
  • 66
  • 462
  • 664