Posting a CVS file - Perl example

An example using Perl to post a CSV file with basic auth credentials.

# file: uploadafile.pl
# call me like this:
# uploadafile.pl --url="https://instance.service-now.com/sys_import.do?sysparm_import_set_tablename=dloo_test&sysparm_transform_after_load=true" 
# --uploadfile=/Users/davidloo/Desktop/test_files/test_users.csv
#
# the "sysparm_transform_after_load=true" parameter instructs the import set to transform immediately after loading
#
use strict;
use LWP::UserAgent;
use HTTP::Request::Common;
use Getopt::Long;
use File::Basename;
 
my ( $o_url, $o_fqn );
GetOptions(
    "url=s"        => \$o_url,
    "uploadfile=s" => \$o_fqn,
);
 
# mandatory arguments: url
&usage unless ( $o_url && $o_fqn );
 
my $url   = $o_url;
my $fname = $o_fqn;
 
# put timeouts, proxy etc into the useragent if needed
my $ua  = LWP::UserAgent->new();
 
# setup basic authentication credentials
$ua->credentials(
  'demo.service-now.com:443',
  'Service-now',
  'admin' => 'admin'
);
 
my $req = POST $url, Content_Type => 'form-data',
        Content      => [
                submit => 1,
                upfile =>  [ $fname ]
        ];
my $response = $ua->request($req);
 
 
 
if ($response->is_success()) {
    print "OK: ", $response->content;
} else {
    print $response->as_string;
}
 
exit;
 
sub usage {
        printf "usage: %s --url=%s --uploadfile=%s\\n",
                basename($0),'https://....','c:/data/test.csv';
        exit
}