Skip to content

Instantly share code, notes, and snippets.

@jberger
Created April 28, 2021 17:24
Show Gist options
  • Save jberger/4cfbb949e72764448007030d93b2daec to your computer and use it in GitHub Desktop.
Save jberger/4cfbb949e72764448007030d93b2daec to your computer and use it in GitHub Desktop.
use Mojo::Base -strict, -signatures;
use Mojo::UserAgent;
my @content = (qw(this is a test));
my $iterator = sub { shift @content };
# Build a normal transaction
my $ua = Mojo::UserAgent->new;
my $tx = $ua->build_tx(GET => 'http://example.com');
# Start writing directly with a drain callback
my $drain = sub ($content) {
my $chunk = $iterator->();
if (length $chunk) {
$content->write($chunk, __SUB__);
} else {
$content->write('');
}
};
$tx->req->content->$drain;
# Process transaction
$tx = $ua->start($tx);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment