Search code examples
perlcurllwp

convert curl command line to Perl WWW::Curl or LWP


I'm trying to replicate the following command line curl:

curl -F [email protected] 'https://myserver/api/import/data_save.html'

Does anyone have an example in either www::curl or lwp perhaps? I have been trying all day and it's not even worth me posting my tries at this point, it would only confuse things. Thank you!!!


Solution

  • I think you are asking how to submit a form with a file field named file populated with the contents of file myfile.csv.

    use LWP::UserAgent qw( );
    
    my $ua = LWP::UserAgent->new();
    
    my $response = $ua->post('https://myserver/api/import/data_save.html',
       Content_Type => 'form-data',
       Content => [
          file => [ 'myfile.csv' ],
       ],
    );
    
    die($response->status_line)
       if !$response->is_success;
    

    The arguments to $ua->post are documented in HTTP::Request::Common.