Subject: | multipart/* with no boundary |
When multipart/* request is done, boundary for header 'Content-Type' is
defined automatically, but too late, at time when headers are already
prepared to send.
Error is located in LWP::Protocol::http->request() or HTTP::Message
logic, as you wish.
'boundary' part of content-type 'multipart/*' is generated on
HTTP::Message->_content() call, but in LWP::Protocol::http code it's
done only after headers are prepared. So, following lines of code must
be reordered:
my $request_headers = $request->headers->clone;
$self->_fixup_header($request_headers, $url, $proxy);
$request_headers->scan(sub {
my($k, $v) = @_;
$k =~ s/^://;
$v =~ s/\n/ /g;
push(@h, $k, $v);
});
my $content_ref = $request->content_ref;
Here ->content_ref calls ->_content and generates message-content and
sets boundary. So it must be called before $request->headers->clone;
Here located another incorrect behaviour. Some lines bellow
Content-Length header is modified directly in @h, so if after request is
done we call $response->request->header('Content-Length') we get value
what wasn't really send.
Code, on which I obtained this error is followed:
my $ua = new LWP::UserAgent;
my $req = new HTTP::Request( 'POST', $url );
$req->content_type( 'multipart/form-data' );
$req->header( 'Accept' => 'application/json' );
foreach my $file ( @list )
{
my %info = %{ $file->{info} };
my $part = new HTTP::Message;
$part->header( %info );
$part->content( $file->{content} );
$req->add_part( $part );
}
# fixup for error in LWP::Protocol::http.
# They set boundary of header Content-Type after headers are already
serialized.
# Here $req->_content() call is a dirty hack used to solve this problem.
$req->_content();
my $res = $ua->request( $req );