Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Streaming data using dancer

by gsiems (Chaplain)
on Mar 14, 2013 at 17:54 UTC ( #1023517=perlquestion: print w/replies, xml ) Need Help??
gsiems has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,

I have a web-app written using Dancer (version 1.310) that, so far, works beautifully. However, I need to add a route that returns large datasets from a database. Because the datasets are too large to just "run query, grab results, return them" I'm looking at using streaming instead and am having issues getting things to work.

I'm using the following test snippet that uses a simple loop to simulate doing the database retrieval and things aren't working.

get '/test_download' => sub { return send_file ( 'data.csv', streaming => 1, callbacks => { override => sub { my ( $respond, $response ) = @_; my $writer = $respond->(200, [ 'Content-Disposition' = +> 'attachment; filename="data.csv"' ] ); for ( 0 .. 100) { my $line = join ',', $_, qw(1 2 3 foo bar baz 47 3 +.14159); $writer->write ($line . "\n"); } }, }, ); };

The error that I'm getting is: Can't call method "streamed" on an undefined value at /opt/restful/perl5/perlbrew/perls/perl-5.14.2/lib/site_perl/5.14.2/ line 452.

Any ideas what's wrong here?


Replies are listed 'Best First'.
Re: Streaming data using dancer
by Anonymous Monk on Mar 14, 2013 at 19:51 UTC

    You don't have  [ $status, $headers, ] which appears to be the required argument to $respond->(), you need  $respond->( [ 200, ['Content...'] ]);

      It would be nice if that were the problem, however substituting:
      my $writer = $respond->(200, [ 'Content-Disposition' => 'attachment; filename="data.csv"' ] + );
      my $writer = $respond->([200, [ 'Content-Disposition' => 'attachment; filename="data.csv"' ] +] );
      or even just
      my $writer = $respond->([200, ['Content-Type' => 'text']] );
      still doesn't work (same error).

      I wouldn't be suprised to have it be something so simple. Unfortunately, I've only found the one example in the documentation and it's a little sparse (IMO).

        It would be nice if that were the problem ...

        Yes it would have :)

        I see by override|streaming&i=1&n=1&C=0 that dancer doesn't test interface, so its not surprising the documented examples don't work -- report bug upstream, is usually occupied

        Also, you could try searching for examples on github, its not unheard of :)

        Found it! The problem was that, even though I'm over-riding things so that it is sending back the results of the loop rather than an actual file-system file, the file-system file has to exist and dancer needs to be able to access it. Rather than dropping a helpful error message along the lines of "file does not exist" it just gacked.

        We're happy now...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1023517]
Front-paged by tye
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2017-02-23 00:39 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (338 votes). Check out past polls.