Handling commas in a GET request on an array column

Here’s my situation

public class Dto
    public string[] Companies { get; set; }

If I do a GET “/Test?Companies=Microsoft,Tesla”

You can understand that as two different companies. Makes sense! But what if I do this?
GET “/Test?Companies=My Company, LLC”

That’s definitely not two separate companies.

Two solutions I see:

  1. Split arrays into multiple parameters like this, and each parameter is considered an individual item:
    GET “/Test?Companies=Microsoft&Companies=Tesla”

  2. Well, same thing as #1 but in a different URL format which I’ve seen other web servers use:
    GET “/Test?Companies[]=Microsoft&Companies[]=Tesla”

The 3rd solution I don’t love. I’ve seen it referenced here before, which is how to pass complex data for a GET request using JSV.

And the 4th solution would be to use POSTS, which I also don’t like!

Thoughts on how to properly handle this? Are there settings I can use to handle querystrings properly?

The QueryString params are already being parsed by JSV which is how you’re able to send any complex types via QueryString.

Escaping in JSV is the same as CSV where you can quote values that use escape chars, e.g:

/Test?Companies=Microsoft,Tesla,"My Company, LLC"

You can send additional QueryString parameters not on your DTO which your APIs can access from base.Request.QueryString that your API impl can inspect to construct an array.

You could use a Custom Request Binder to override ServiceStack’s default Request Binding.

Otherwise use POST to send complex types properties as JSON.

1 Like