0

I am trying to upload a file to a public API with a code similar to this:

my $ua = LWP::UserAgent->new;
sub uploadbox {
    my $url = "http://host/token";
    my  $response = $ua->put($url, 
        'Content_Type' => 'form-data',
        'Content' => [
            Filedata => [ "$codename.box", "$codename.box", Content_type => 'application/octet-stream' ]

        ]
    );
}
uploadbox();

This code runs, and exits without uploading anything ( the uploaded files are 300MB big, so it shoud take time).

Am I passing the right parameters to the put subroutine ? How to further debug this ?

Manu
  • 426
  • 3
  • 17
  • That code wouldn't do anything because `uploadbox` is never called. All it does it compile `LWP::UserAgent` and its dependent modules, create an object, and then exit – Borodin Feb 19 '16 at 21:13
  • I am calling uploadbox(); the next line, just updated the code – Manu Feb 19 '16 at 21:21
  • 2
    Maybe look at the `$response` to see what the error is? – cjm Feb 19 '16 at 21:25
  • 2
    Start by making it not silent... Print the client-side error! `die $response->status_line if !$response->is_success;` – ikegami Feb 19 '16 at 22:38
  • thanks, this led a HTTP status code of 400, but I could not find why – Manu Feb 25 '16 at 00:00

2 Answers2

0

I like to debug LWP::UserAgent scripts using LWP::ConsoleLogger::Easy. (Disclaimer: this is one of my own modules).

use LWP::ConsoleLogger::Easy qw( debug_ua );
my $ua = LWP::UserAgent->new;
debug_ua( $ua );

# insert the rest of your code here

You'll now get a huge amount of debugging information from both the request and the response printed to your terminal. That should hopefully give you a good starting point to figure out what's going on.

oalders
  • 5,239
  • 2
  • 23
  • 34
0

In the end I just decided to use curl, and I get a dynamic status line for free

sub uploadbox {
    my ($url) = @_;
    my $curl = "curl -X PUT $url --upload-file $codename.box";
    $OUTPUT_AUTOFLUSH = 1;
    open(CURL,  '-|', $curl,) or die "error: $ERRNO";
    while (<CURL>) { say; }
 }

not the code I am most proud of but ...

Manu
  • 426
  • 3
  • 17