public
Last active

  • Download Gist
multipart_response.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#!/usr/bin/perl
use strict;
use warnings;
 
use HTTP::Response;
 
my $response = HTTP::Response->new(
200, 'OK', [ 'Content-Type' => 'multipart/form-data' ]
);
 
$response->protocol('HTTP/1.1');
$response->date(time);
$response->server('Foo/1.0');
 
my $name = HTTP::Message->new([
'Content-Type' => 'text/plain; charset=UTF-8',
'Content-Disposition' => 'form-data; name="name"',
], 'John Doe');
 
$response->add_part($name);
 
my $note = HTTP::Message->new([
'Content-Type' => 'text/plain; charset=UTF-8',
'Content-Disposition' => 'form-data; name="note"',
], <<'NOTE');
Resources:
o http://search.cpan.org/dist/HTTP-Message/lib/HTTP/Message.pm
o http://search.cpan.org/dist/HTTP-Message/lib/HTTP/Response.pm
o http://tools.ietf.org/html/rfc2388
o http://tools.ietf.org/html/rfc2616
NOTE
 
$response->add_part($note);
 
my $blob = HTTP::Message->new([
'Content-Type' => 'application/octet-stream',
'Content-Disposition' => 'form-data; name="blob"; filename="blob.bin"',
]);
$blob->add_content('a chunk');
$blob->add_content(' of data');
 
$response->add_part($blob);
 
print $response->as_string;
 
__END__
HTTP/1.1 200 OK
Date: Thu, 24 Nov 2011 10:03:25 GMT
Server: Foo/1.0
Content-Type: multipart/form-data; boundary=xYzZY
 
--xYzZY
Content-Type: text/plain; charset=UTF-8
Content-Disposition: form-data; name="name"
 
John Doe
--xYzZY
Content-Type: text/plain; charset=UTF-8
Content-Disposition: form-data; name="note"
 
Resources:
o http://search.cpan.org/dist/HTTP-Message/lib/HTTP/Message.pm
o http://search.cpan.org/dist/HTTP-Message/lib/HTTP/Response.pm
o http://tools.ietf.org/html/rfc2388
o http://tools.ietf.org/html/rfc2616
 
--xYzZY
Content-Type: application/octet-stream
Content-Disposition: form-data; name="blob"; filename="blob.bin"
 
a chunk of data
--xYzZY--

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.