views:

70

answers:

6

Each parameter in a URL can have multiple values. How can I separate them? Here's an example:

http://www.example.com/search?queries=cars,phones

So I want to search for 2 different things: cars and phones (this is just a contrived example). The problem is the separator, a comma. A user could enter a comma in the search form as part of their query and then this would get screwed up. I could have 2 separate URL parameters:

http://www.example.com/login?name1=harry&name2=bob

There's no real problem there, in fact I think this is how URLs were designed to handle this situation. But I can't use it in my particular situation. Requires a separate long post to say why... I need to simply separate the values.

My question is basically, is there a URL encodable character or value that can't possibly be entered in a form (textarea or input) which I can use as a separator? Like a null character? Or a non-visible character?

UPDATE: thank you all for your very quick responses. I should've listed the same parameter name example too, but order matters in my case so that wasn't an option either. We solved this by using a %00 URL encoded character (UTF-8 \u0000) as a value separator.

+4  A: 

The standard approach to this is to use the same key name twice.

http://www.example.com/search?queries=cars&queries=phones

Most form libraries will allow you to access it as an array automatically. (If you are using PHP (and making use of $_POST/GET and not reinventing the wheel) you will need to change the name to queries[].)

David Dorward
I mentioned in my update that you're correct this was an option too, but in my case I really need to just separate values within a parameter value. Order matters and parameters with the same name will have different separated values (sometimes with just a different order)
at
+1  A: 

Come up with your own separator that is unlikely to get entered in a query. Two underscores '__' for example.

JungleFreak
That's essentially what I was asking for, but I wanted something that would be impossible to get entered into a standard html form element.
at
A: 

easiest thing to do would be to use a custom separator like [!!ValSep!!].

Vinay B R
A: 

You can give them each the same parameter name.

http://www.example.com/search?query=cars&query=phones

The average server side HTTP API is able to obtain them as an array. As per your question history, you're using JSP/Servlet, so you can use HttpServletRequest#getParameterValues() for this.

String[] queries = request.getParameterValues("query");
BalusC
Yes, this doesn't work for me either because order matters, I explained in another reply in slightly more detail. I'm happy to see you remember my other questions though! Thank you for all your help! I did find a good solution, I think, to this issue (question updated).
at
+1  A: 

Why not just do something like "||"? Anyone who types that into a search area probably fell asleep on their keyboard :} Then just explode it on the backend.

malonso
maybe you're right :). But this is for a standard widget mechanism that will be used in a thousand different situations.
at
Ahhh, ok that changes things. Sorry about that.
malonso
+1  A: 

Just URL-encode the user input so that their commas become %2C.

Mark Cidade