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

Hi, I am trying to push files in a given directory, passed as a parameter, recursively. The file need to be base64 encoded and the destination accepts the file in a specific SOAP request. When I print the file into a notepad, it looks good but thru CURL.exe run through curl command, the response I am getting is ... Malformed content (from client), which means the request is not well-formed. I believe there is something I am missing here. Please see the code below and suggest your comments for making it to work, please.
use strict; use File::Find; use MIME::Base64; sub loadFiles(); #udf sub mySub(); #udf my @files = (); my $dir = shift || die "Argument missing: directory name\n"; my $finalLoc; my $filePath; my $fileContents; my $base64EncFile; my $domain="TEST"; my $devFilePath; my $deviceDir; my $position; my $user="USER"; my $encPwd="VVNFUjEyMw=="; my $decPwd; loadFiles(); #call foreach (@files) { $filePath = $_; #replace / to \ in the directory path $filePath =~ s/\//\\/g; $devFilePath = $_; # replace \ to / when setting a file path into the target location $devFilePath=~ s/\\/\//g; $position = index($devFilePath, "RPDM"); #take a substring after the RPDM location and add that to the stri +ng below $deviceDir = "local:///".substr($devFilePath, $position); #open a file using FILE handle open( FILE, "< $filePath" ); # take all file contents into a variable $fileContents = do { local $/; <FILE> }; #base-64 encode the file contents and store into a variable $base64EncFile = encode_base64($fileContents); #flatten the string by removing <CR><LF> characters $base64EncFile =~ s/[\x0A\x0D]//g; #save the web service request into a variable my $dpString = "<env:Envelope xmlns:env='http://schemas.xmlsoap.or +g/soap/envelope/' xmlns:dp='http://www.datapower.com/schemas/manageme +nt'><env:Body><dp:request domain='$domain'><dp:set-file name='$device +Dir'>".$base64EncFile."</dp:set-file></dp:request></env:Body></env:En +velope>"; #decode the above encoded password $decPwd=decode_base64($encPwd); #make a curl command to submit the web-service request at the dest +ination end-point system('C:\\apps\\curl-7.15.0\\curl.exe -# -k -u admin:$decPwd --d +ata-binary $dpString https://host:1234/services/endPoint'); close(FILE); } sub loadFiles() { find( \&mySub, "$dir" ); #custom subroutine find, parse $dir } # following gets called recursively for each file in $dir, check $_ to + see if you want the file! sub mySub() { push @files, $File::Find::name if (/(\.xml|\.xsl|\.xslt|\.ffd|\.dpa|\.wsdl|\.xsd)$/i) ; # modify the regex as per your needs or pass it as another +arg }
Appreciate the help. Thank you.

Replies are listed 'Best First'.
Re: Using Windows Curl.exe in Perl
by syphilis (Archbishop) on Apr 24, 2012 at 23:30 UTC
    system('C:\\apps\\curl-7.15.0\\curl.exe -# -k -u admin:$decPwd --data-binary $dpString https://host:1234/services/endPoint');

    In order to have $decPwd and $dpString interpolated correctly, I think you need to enclose the system command in *double* quotes, not *single* quotes:

    system("C:\\apps\\curl-7.15.0\\curl.exe -# -k -u admin:$decPwd --data-binary $dpString https://host:1234/services/endPoint");

    You could also try:

    system('C:\\apps\\curl-7.15.0\\curl.exe', '-#', '-k', '-u', "admin:$decPwd", '--data-binary', "$dpString", 'https://host:1234/services/endPoint');

    Cheers,
    Rob
      Thanks Rob. That worked! Have a follow-up question though - for files over 100kb, I am getting an error: Can't spawn "cmd.exe": No such file or directory Looked around to see but could not find anything that works. Can you please throw some light here, please. Thanks again.