[dancer-users] Accepting JSON in a POST request

Gabor Szabo gabor at szabgab.com
Mon Aug 24 10:10:18 BST 2015


On Mon, Aug 24, 2015 at 11:55 AM, Andrew Beverley <andy at andybev.com> wrote:

> On Mon, 2015-08-24 at 11:31 +0300, Gabor Szabo wrote:
> > When I use Angular to send a POST request to the Dancer back-end, by
> > default it send
> >
> > 'CONTENT_TYPE' => 'application/json;charset=UTF-8',
> >
> > and the data looks like this:
> >
> > 'body' => '{"foo":23,"bar":19}',
> >
> > and Dancer2 does not parse this.
>
> I think a serializer should do what you want, but you need to explicitly
> enable
> one:
>
> https://metacpan.org/pod/Dancer2::Manual#Serializers1
>
> Andy
>
>

I have just tried to set

   set serializer => 'JSON';

that caused another issue. Now when I return the main page which is plain
HTML, I get an error

 Failed to serialize the request: hash- or arrayref expected (not a simple
scalar, use allow_nonref to allow this) at
/Users/gabor/perl5/perlbrew/perls/perl-5.22.0_WITH_THREADS/lib/site_perl/5.22.0/JSON.pm
line 154. in (eval 153) l. 1

and I don't get the page back.

Then I tried to trick it and loaded the HTML page before enabling the
serializer, then switch it on, let the server restart and sent the
Ajax POST request. That worked, but I am now with the problem of displaying
the initial HTML page.
Reading the docs you linked to I thought it should just send out the plain
HTML page as it is a plain scalar. I am not sure why does it fail.

Gabor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20150824/d345f77c/attachment-0001.html>


More information about the dancer-users mailing list