0

Is there a standard for handling an array of integers like the following:

http://localhost:3001/?tag_ids=7,10,45

or

http://localhost:3001/?tag_ids=[7,10,45]

I feel like I've written boiler-plate every 6 months and would much rather use some library.

timpone
  • 19,235
  • 36
  • 121
  • 211
  • Probably JSON is the simplest thing. – Pointy Jan 11 '18 at 00:18
  • Related https://stackoverflow.com/questions/6243051/how-to-pass-an-array-within-a-query-string – Tulir Jan 11 '18 at 00:21
  • hmm...... yeah, might be the way to go. like: `encodeURI(["blue","some"]);` gives me `"blue","some"`. yeah, that makes sense - the boilerplate is fine and I pretty much always do this as integers – timpone Jan 11 '18 at 00:23

1 Answers1

0

A number of server-side scripting languages like PHP expect each value to be in a separate parameter, with the parameter name ending with [].

tag_ids[]=7&tag_ids[]=10&tag_ids[]=45

They recognize this syntax and collect all the values into an array. In PHP this would be $_GET['tag_ids'] = [7, 10, 45].

You can also optionally put strings inside the []; in this case, the server language will put them into its keyed data structure (associative array in PHP, dictionary in Python) with those as the keys.

You should call encodeURIComponent on tag_ids[], so they'll actually end up looking like tag_ids%5B%5D.

Barmar
  • 741,623
  • 53
  • 500
  • 612