I have a problem working with the built in Perl Dancer serializer for JSON and JSON arrays.
I activated the serializer in the app.pl file:
#!/usr/bin/env perl
use Dancer;
use main;
set serializer => 'JSON';
dance;
In the module itself I tested the JSON parsing like this:
post '/test/' => sub {
my $params = request->params;
debug('Test: ', $params);
};
Now I wanted to make sure the JSON gets parsed as expected, so I tried using cURL to understand the way the serializer works:
curl -H "Content-Type: application/json" -X POST http://localhost:3000/test/ -d '{ "Name" : "foo", "email" : "bar" }'
The result was as expected:
Test: {'Name' => 'foo','email' => 'bar'}
But trying to send an array:
curl -H "Content-Type: application/json" -X POST http://localhost:3000/test/ -d '[{ "Name" : "foo", "email" : "bar" }]'
Resulted in:
Test: {}
I expected the serializer to return an array reference, but it seems like it returns an empty hash. I tried using the serializer the other way around, but encoding JSONs seems to work as expected. What have I done wrong?