Created
April 28, 2021 17:24
-
-
Save jberger/4cfbb949e72764448007030d93b2daec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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