Search code examples
resturlurl-encoding

What character may (and should) I use as internal separators for my field values in URL query string?


I am developing a web application that uses both URL routes and query string parameters. Some of my parameters contain complex lists and require some internal separators. For example I need a filter to support several entities, like usernames and dates.

Basically I need to encode the following JSON-object:

{
"users" : ["alex","mike","john"],
"datefrom" : "2013-12-01"
}

as one querystring parameter (filter).

I have several ideas:

_http://hostedapplication/?filter=users@alex,mike,john;datefrom@2013-12-01

_http://hostedapplication/?filter=users|alex,mike,john!datefrom|2013-12-01

So, what are the typical approaches here? What characters I might use for my separators?


Solution

  • For a REST interface, it's pretty common to use GET parameters as filters for a GET request. So the top-level filter params you could use as GET parameters, and if you go a level deeper you could indeed use separators for Arrays.

    In that case, there is not really a standard way to do it, REST doesn't have rules for that, so you can decide for yourself. Personally, i like comma's:

    ?users=alex,mike,john&datefrom=2013-12-01
    

    Just note that if any of the users have usernames that contain URL control characters, you should url encode them.