Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

separating values in a URL, not with an &

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.

like image 714
at. Avatar asked Sep 17 '10 20:09

at.


People also ask

How do you separate values in a URL?

URL parameters are made of a key and a value, separated by an equal sign (=). Multiple parameters are each then separated by an ampersand (&).

How do you query parameters in a URL?

Query parameters are a defined set of parameters attached to the end of a url. They are extensions of the URL that are used to help define specific content or actions based on the data being passed. To append query params to the end of a URL, a '? ' Is added followed immediately by a query parameter.

Which method will pass the values via URL?

Submitting form values through GET method A web form when the method is set to GET method, it submits the values through URL. So we can use one form to generate an URL with variables and data by taking inputs from the users.

What is a query string in a URL?

A query string is a set of characters tacked onto the end of a URL. The query string begins after the question mark (?) and can include one or more parameters. Each parameter is represented by a unique key-value pair or a set of two linked data items. An equals sign (=) separates each key and value.


2 Answers

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[].)

like image 68
Quentin Avatar answered Sep 21 '22 18:09

Quentin


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");
like image 31
BalusC Avatar answered Sep 22 '22 18:09

BalusC