-2

I heard that its a hack to pass array in query parameters like

http:site.com?a[]=5&a[]=6

I know PHP support array in query parameters so on server side you can get something like

$_GET[a] = [5,6]

Does other programming languages support this like PHP do? Is it hack to pass array in such way? If yes then what is best practice to pass array in GET parameters?

IMO: Its feature not a hack still want to get opinion of Community

Bhaskar Dabhi
  • 841
  • 1
  • 11
  • 28

2 Answers2

1

That's not a hack, it's an awesome feature!

I think people are saying this because it's ugly if your users see it in the URL.

What you can do is concat your data, then explode it server side.

http:site.com?a=5_6

explode('_', $a);

Elie Faës
  • 3,215
  • 1
  • 25
  • 41
0

Is it a hack to send array in query parameters in get?

It's a non-standard addition to application/x-www-form-urlencoded (which wasn't all that clearly defined in the first place), so yes, it is a hack.

Whether it is a clever hack or an ugly hack is largely a matter of opinion. There are pros and cons.

Does other programming languages support this like PHP do?

Most languages don't have HTTP processing baked in at such a fundamental level. PHP started life as a library for doing CGI-style things with and it shows. You really should be looking at libraries for processing form data and not languages here.

Most will let you expand duplicate keys in query strings without requiring that the name include [] (which is verbose and hard to read) for it to work. The limitation of this is that you can't explicitly specify the array index in the data.

There are implementations of the PHP style though. body-parser lets you use that syntax when you specify extended: true. Perl's PHP::ParseStr implements it, although doesn't integrate it with code to extract the string from the request.

If yes then what is best practice to pass array in GET parameters?

If you're using PHP, then use PHP's syntax for it.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335