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

Dear friends, I am trying to download pdf file by using lwp::useragent POST method. But I have no idea how to do. Can someone teach me ? Thank you for your attention. Below is my original script. I can only get the response web site but not the file.
#!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; my $base = 'http://www.tcichemicals.com/eshop/en/ap/catalog/coa_result +/?page=coa'; my $filename = "test.pdf"; my $params = { product_number => 'E0393', lot_number => 'DY5BB' }; my $agent = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0}); push @{$agent->requests_redirectable}, 'POST'; print "@{$agent->requests_redirectable}". "\n"; my $response = $agent->post("$base", $params); print $response->content; my $response = $agent->post("$base", $params); print $response->content; while (my $wait = $response->header('Retry-After')) { print STDERR "Waiting ($wait)...\n"; sleep $wait; $response = $agent->get($response->base); } die $response->status_line if !$response->is_success; my $file = $response->decoded_content( charset => 'none' ); #my $md5_hex = md5_hex($filename); #print "$md5_hex\n"; #my $save = "$file"; open (SAVE, ">", $filename) || die "open file error"; print SAVE $file; close SAVE;

Replies are listed 'Best First'.
Re: download file by using lwp::useragent POST method
by holli (Abbot) on Dec 12, 2017 at 08:24 UTC
    Assuming your URL and parameters are correct, chances are the server doesn't respond correctly because of the POST method. Usually, requesting files is done via GET.


    holli

    You can lead your users to water, but alas, you cannot drown them.
Re: download file by using lwp::useragent POST method
by Corion (Patriarch) on Dec 12, 2017 at 08:10 UTC

    You already have some code.

    What part of it is not working, and how does it fail to do what you need?

      If I surf the web site manually
      (http://www.tcichemicals.com/eshop/en/ap/catalog/coa_result /?page=coa) and post with the two variables,
      product_number=E0393,product_number=DY5BB
      the browser will prompt file download dialog
      but my perl script code
      $response->decoded_content( charset => 'none' );
      right now only returns the web site content, not the desired file.
      Is it possible to ask lwp to return the file content, not the web site content?

        The "site content" and the "file content" are the same.

        What you seem to get back is maybe something else and not the file download. Maybe you are not sending the same data as your browser does. Maybe there are cookies or other additional parameters that are hidden that need to be sent.

        Consider using the "Developer Tools" of your browser of choice or a network sniffer to inspect what actually gets sent over the wire. Replicate that in your Perl script.

        Maybe the returned page contains redirecting Javascript that sends your browser to the real file download URL.

        There are many things that might be true in your situation but you are the only person to look at those.