Accepting JSON in a POST request
Hi, When I send a POST request with JQuery or with a plan HTML form I see 'CONTENT_TYPE' => 'application/x-www-form-urlencoded; charset=UTF-8', and the data looks like this: 'body' => 'foo=23&bar=19', If I understand it correctly then this is what Dancer turns into the values accessible via param() 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. Shouldn't Dancer2 look at the content type of the POST request and handle the data accordingly? Or is that the job of PSGI? Do I need to tell something to Dancer2 or to PSGI to do this? regards Gabor
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
On Mon, Aug 24, 2015 at 11:55 AM, Andrew Beverley <andy@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
On Mon, 2015-08-24 at 12:10 +0300, Gabor Szabo wrote:
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)
Ah yes, the auto-serializer's behaviour used to be that it would auto-detect and act accordingly. That changed in a recent version of Dancer2, full details here: http://advent.perldancer.org/2014/11 In short, you need a separate app, one with a serializer and one without.
Reading the docs you linked to I thought it should just send out the plain HTML page as it is a plain scalar.
Looks like the docs need updating... Andy
participants (2)
-
Andrew Beverley -
Gabor Szabo