如何在 perl dancer 应用程序中防止一条路线的序列化



我有一个perl dancer应用程序(提供一个rest api(,它可以很好地处理JSON(反(序列化。现在我需要一条额外的特殊路由,它提供了一个(动态创建的(csv 文件供下载。

下面是示例代码:

#!/usr/bin/env perl
use Dancer2;
set serializer => 'JSON';
get '/normal' => sub {
    { 'I say ' => 'the json serializer works' };
};
get '/download' => sub {
    content_type 'text/csv';
    return  generateCsv();
};
sub generateCsv {
    return '
    1,2,3
    4,5,6
    ';
}
dance;

发送到客户端的响应没有正文,只有一个 http 标头(具有正确的内容类型(

$> curl  -I  http://localhost:3000/download
HTTP/1.0 200 OK
Date: Fri, 23 Mar 2018 10:10:14 GMT
Server: Perl Dancer2 0.205002
Server: Perl Dancer2 0.205002
Content-Length: 0
Content-Type: text/csv

舞者序列化者对此不满意:

Failed to serialize content: hash- or arrayref expected 
(not a simple scalar, use allow_nonref to allow this) 
at /usr/local/share/perl/5.22.1/Dancer2/Serializer/JSON.pm line 40. 
in /usr/local/share/perl/5.22.1/Dancer2/Core/Response.pm 

我在 Dancer 文档或源代码中找不到有关allow_nonref内容的任何内容。

有人对我有提示吗?

使用 send_as

get '/download' => sub {
    send_as Mutable => generateCsv();
};

我发现send_file也可以:

get '/download' => sub {
        send_file (&generateCsv(), content_type => 'text/csv', filename => 'articleEbayStatus.csv');
};

最新更新