Search code examples
perlparamdancer

How to get multi param with Dancer2?


I'm trying to get array in param, everything works well with single data, but not for array...

client-side (list of parameters sent from the browser):

list[1] null
list[2] 2
list[3] 10
list[4] null

server-side:

any ['get','post'] => '/save_list' => sub {
  my $items = param ('list');
  #result = null, mb is  only the first element
  #my $items = param ('list[]');
  #result = null
  #my @items = param ('list[]');
  #result = empty
  #my @items = param ('list');
  #result = empty   
};

Where Am I Wrong?

My version Dancer2-0.155004


Solution

  • The DSL keyword parameters will return a Hash::MultiValue object that you can use to access those in Dancer2.

    any ['get','post'] => '/save_list' => sub {
      my @items = parameters->get_all('list');
      foreach my $item ( @items ) {
        do_stuff($item);  
      }
    };