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

I am having a problem posting a file to Amazon MWS service using perl. The problem is that they require an MD5 hash along with the file, and some other parameters. I am getting syntax errors and I just can't seem to find the right way to form this post. Can you take a look and see where I am going wrong. Perhaps someone has some experience with Amazon MWS services. Thank you very much!
use LWP::UserAgent; use HTTP::Request::Common; use Digest::MD5::File qw(file_md5_hex); use File::Slurp; #text file to upload my $text_file = 'file.txt'; #calulate Content-MD5 Header my $md5hash = file_md5_hex($text_file); #read file my $file_contents = read_file($text_file); #fields to post my %post_parameters = ( 'a' => 'red', 'b' => 'blue', 'c' => 'green' ); $ua = LWP::UserAgent->new(); $ua->agent('Blah/Perl'); $req = new HTTP::Request 'POST','http://amazonmws.blah'; $req->content_type('text/xml; charset=iso-8859-1'); $req->header( 'Content-MD5' => $md5hash ); $req->content([%post_parameters]); $req->add_content($file_contents); $res = $ua->request($req);

Replies are listed 'Best First'.
Re: Posting files to Amazon MWS using LWP
by BrowserUk (Patriarch) on Nov 29, 2011 at 19:32 UTC

    The HTTP::Request::content() method is described as: "$r->content( $bytes ) ... Note that the content should be a string of bytes.", so what do you expect it to do with an anonymous array containing a list of strings:  $req->content([%post_parameters]);?

    You should be using the LWP::UserAgent::post() method: "$ua->post( $url, \%form )".


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      I have seen that method, but it is not clear on how to add the additional MD5 header I need to add, as well as adding the contents of the file. Would you be able to expand a little on your example to show that?

        Completely untested, but my interpretation of the docs suggests it should look something like this:

        use LWP::UserAgent; use HTTP::Request::Common; use Digest::MD5::File qw(file_md5_hex); use File::Slurp; #text file to upload my $text_file = 'file.txt'; #calulate Content-MD5 Header my $md5hash = file_md5_hex($text_file); #read file my $file_contents = read_file($text_file); #fields to post my %post_parameters = ( 'a' => 'red', 'b' => 'blue', 'c' => 'green' ); $ua = LWP::UserAgent->new(); $ua->agent('Blah/Perl'); $ua->post( 'http://amazonmws.blah', Content_Type =>'form-data', Content-MD5 => $md5hash, Content => [ file => [ $file_contents, $text_file ], %post_parameters ], ); print STDERR $rsp->status_line;

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Posting files to Amazon MWS using LWP
by Your Mother (Archbishop) on Nov 29, 2011 at 18:59 UTC

    I've never used the MWS but after a quick skim of the dev guide I think your reading of the spec is incorrect(?). This snippet from their docs seems to line-up with the AWS/APA/BlahBlah.vπ interface neatly–

    SignatureMethod—The HMAC hash algorithm you are using to calculate your signature, either HmacSHA256 or HmacSHA1

    Good news is you should be able to pull some code from or reuse URI::Amazon::APA. Bad news, you need to read the spec again. Might ask around the forums if one of the CPAN modules (none of which seems to contain "MWS") works for this.

      Actually the MD5 hash is one part that is working here. I think the problem is in the LWP syntax with adding the parameters hash and the the file contents. Doesn't like both of those there. I just can seem to find any examples of how to add parameters and the file contents to a post.