[dancer-users] Incorrect attempts to serialize HTML

Sawyer X xsawyerx at gmail.com
Thu Oct 9 18:43:02 BST 2014

On Thu, Oct 9, 2014 at 1:47 PM, Andrew Beverley <andy at andybev.com> wrote:

> Hi guys,

Hi Andrew. :)

> I apologise in advance that this is rather vague, but I'm hoping that
> someone might be able to point me in the right direction of where I
> should be looking.

Not at all! That is an excellent question.

> [...] so I won't go into the details here, but I wondered whether
> someone can point me to the relevant parts of the Dancer2 code that I
> should start poking around in (in particular the code which detects
> whether to serialize the data).

You can see that Dancer2::Core::Request checks for a supported content
type. The line is:

    return unless $self->serializer->support_content_type($content_type);

However, in Dancer2::Core::Response, it doesn't check for a supported
content type in the request in order to serialize a response back. This
would be the correct behavior since a serializer basically says "I would
like to serialize my responses" and not "I would like to serialize my
responses IF the request was made with a header". The reason it ignores any
request header in the response is because it is, by definition, a *request*
header. This means it only relates to the request, not the response.

If you would like to return HTML and serialized information, you have two
1. Separate the code that accepts and returns serialized data into two
different Dancer2 apps.
2. Don't use a serializer, and call the to_json and from_json when *you*
decide it's the right time for it.

I hope this answer helps.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20141009/d43d8aed/attachment.html>

More information about the dancer-users mailing list