jeclark2006 has asked for the wisdom of the Perl Monks concerning the following question:

There are a number of examples of creating multipart form data for sending to a HTTP server.

I found very few, server side parsing examples. This ranged from getting a message that CGI.pm is delisted in the standard perl suite, to finding an HTTP::MultiPartParser library function, etc.

However when using the latter, HTTP::MultiPartParser functions, a 'boundary' pattern was required to be given when creating the object.

So, upon receiving content, how does one find the 'boundary' to then use HTTP::MultiPartParser to then parse the file?

Could someone point me to an example where perl is used to receive the file selected via the multipart form, and then parsed out to filename, and file content segments.

Thanks.

Replies are listed 'Best First'.
Re: Perl parsing of multipart content
by daxim (Curate) on Jun 14, 2019 at 07:32 UTC
    See Plack::Request methods "parameters" and "uploads". This should be very familiar to you if you know CGI.pm.

    Server (this is one line):

    plackup -MPlack::Request -MDDP -e'my $app = sub { my ($env) = @_; my $ +req = Plack::Request->new($env); p $req->parameters; p $req->uploads; + return [200,[],[]]; };'

    Client:

    curl -v -F somekey=somevalue -F upload=@somefile http://localhost:5000

    HTTP trace:

    POST / HTTP/1.1 Host: localhost:5000 User-Agent: curl/7.65.0 Accept: */* Content-Length: 322 Content-Type: multipart/form-data; boundary=------------------------84 +f03d39abacb4c6 --------------------------84f03d39abacb4c6 Content-Disposition: form-data; name="somekey" somevalue --------------------------84f03d39abacb4c6 Content-Disposition: form-data; name="upload"; filename="somefile" Content-Type: application/octet-stream somefilecontent --------------------------84f03d39abacb4c6-- HTTP/1.0 200 OK Date: Fri, 14 Jun 2019 07:22:11 GMT Server: HTTP::Server::PSGI Content-Length: 0

    Abbreviated server output:

    Hash::MultiValue { public methods (24) : … private methods (2) : … internals: { somekey "somevalue" } } Hash::MultiValue { public methods (24) : … private methods (2) : … internals: { upload Plack::Request::Upload } } 127.0.0.1 - - [14/Jun/2019:09:22:11 +0200] "POST / HTTP/1.1" 200 0 "-" + "curl/7.65.0"
Re: Perl parsing of multipart content
by holli (Abbot) on Jun 14, 2019 at 07:54 UTC
    So, upon receiving content, how does one find the 'boundary' (...)?
    It is the responsibility of the client application to tell you:
    Content-Type: multipart/form-data; boundary=???
    where ??? is the boundary, obviously.


    holli

    You can lead your users to water, but alas, you cannot drown them.

      Some more information about the boundary parameter: Re^5: Using Net::SMTP to send email attachments. Yes, that posting is about emails. But HTTP has stolen MIME from email, and so came the boundary, MIME types, and much more to HTTP.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Perl parsing of multipart content
by hippo (Archbishop) on Jun 14, 2019 at 08:34 UTC
    getting a message that CGI.pm is delisted in the standard perl suite

    Sure, so it becomes like the other 99.9% of the modules on CPAN. Download, install, use.

    Could someone point me to an example where perl is used to receive the file selected via the multipart form, and then parsed out to filename, and file content segments.

    See the CGI::Lite example or the CGI.pm one. If you need more than these then perhaps you could provide an SSCCE so we can see where you are getting stuck?

Re: Perl parsing of multipart content
by Corion (Patriarch) on Jun 14, 2019 at 06:00 UTC

    Why not just install CGI and use its parsing?