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

What am I missing here? here is a sample url and filename.

| https://www.sec.gov/Archives/edgar/data/1897245/0001493152-23-024253.txt | /temp/edgar/0001493152-23-024253

{ my $SQL = "select url,filename from linktable"; my $query = $dbh->prepare($SQL) or die "Prepare".$dbh->errstr; $query-> execute() or die "".$dbh->errstr; while (my $ref = $query->fetchrow_hashref()) { my $ua = LWP::UserAgent->new(timeout => 10); $ua->default_header('Accept-Encoding' =>$mess = HTTP::Message->new()); $ua->default_header( USER_AGENT =>'COMPANY 'email@email.com' ); #print "$row[0] $row[1]\n"; #Coming back blank, so $ref isn't being as +signed my $res = $ua->mirror( $ref[0], $ref[1] ); } $query->finish; }

Replies are listed 'Best First'.
Re: Using a Fetchrow with LWP
by 1nickt (Canon) on Jul 19, 2023 at 00:24 UTC

    Among other things, you're attempting to fetch a hashref per row but then trying to access it as if it were an arrayref. Meanwhile $ref[0] refers to an array @ref, which you don't have. I guess you're not using strict or warnings, why not?


    The way forward always starts with a minimal test.
      This is the error I get when trying to use an array reference. A URI can't be a ARRAY reference

        See the example I posted here.


        The way forward always starts with a minimal test.
Re: Using a Fetchrow with LWP
by 1nickt (Canon) on Jul 19, 2023 at 00:55 UTC

    Try this.

    use strict; use warnings; use DBD::SQLite; use HTTP::Tiny; # Test DB setup { my $dbh = DBI->connect('dbi:SQLite:dbname=/tmp/testdb','',''); $dbh->do('CREATE TABLE linktable (url TEXT NOT NULL, file VARCHAR( +255) NOT NULL)'); $dbh->do('INSERT INTO linktable VALUES ("https://www.sec.gov/Archi +ves/edgar/data/1897245/0001493152-23-024253.txt", "/tmp/edgar/0001493 +152-23-024253")'); } #--------------------------------------------# my $dbh = DBI->connect('dbi:SQLite:dbname=/tmp/testdb','',''); my $sql = 'SELECT url, file FROM linktable'; my $sth = $dbh->prepare($sql); $sth->execute or die "" . $dbh->errstr; my $ua = HTTP::Tiny->new( default_headers => { USER_AGENT => 'COMPANY email@email.com', }); while ( my $row = $sth->fetchrow_arrayref ) { my ($url, $file) = ($row->[0], $row->[1]); my $resp = $ua->mirror($url, $file); if ( $resp->{success} ) { print "OK\n"; } else { print "Failure: $resp->{status}, $resp->{reason}\n"; } } __END__

    Hope this helps


    The way forward always starts with a minimal test.
      Getting there...

      Now I getting a 403 error because it isn't sending the right headers that sec.gov accepts.

      I'll update that part of the code.

      I should have included that part of the code.

      Up above, there is this

      use Parent:HTTP::Message; $mess=HTTP::Message->new(); $mess=encode(gzip,deflate);

      It is just printing "failure , " with no error message now.

      so this works:
      use File::Fetch; use LWP::UserAgent (); use DBI; use parent 'HTTP::Message'; $mess = HTTP::Message->new(); $mess->encode(gzip,deflate); $filename='/temp/edgar/workfile.txt'; unlink ($filename); $url='https://www.sec.gov/Archives/edgar/data/1869467/0000919574-23-00 +4048.txt'; my $ua = LWP::UserAgent->new(timeout => 10); $ua->default_header('Accept-Encoding' =>$mess = HTTP::Message->new()); $ua->default_header( USER_AGENT =>'COMPANY admin@example.com' ); print "Now downloading the file...\n"; my $res = $ua->mirror( $url, $filename );
      but this doesn't...
      use LWP::UserAgent (); use DBI; use parent 'HTTP::Message'; $mess = HTTP::Message->new(); $mess->encode(gzip,deflate); my $ua = LWP::UserAgent->new(timeout=>10); $mess = HTTP::Message->new(); $mess->encode(gzip,deflate); $ua->default_header('Accept Encoding'=>$mess=HTTP::Message->new()); $ua->default_header( USER_AGENT =>'COMPANY admin@example.com' ); my $SQL = "select url,filename from linktable"; my $sth = $dbh->prepare($SQL) or die "Prepare".$dbh->errstr; $sth-> execute() or die "".$dbh->errstr; while (my $row = $sth->fetchrow_arrayref) { my ($url,$filename)= ($row->[0],$row->[1]); print "\n$row[0] $row[1]\n"; my $resp = $ua->mirror( $url,$filename); if ( $resp->{success} ) { print "OK\n"; } else { print "Failure: $resp->{status}, $resp->{reason}\n"; } }

        "but this doesn't..."

        use LWP::UserAgent (); use DBI; use parent 'HTTP::Message'; $mess = HTTP::Message->new(); $mess->encode(gzip,deflate); my $ua = LWP::UserAgent->new(timeout=>10); $mess = HTTP::Message->new(); $mess->encode(gzip,deflate); $ua->default_header('Accept Encoding'=>$mess=HTTP::Message->new()); $ua->default_header( USER_AGENT =>'COMPANY admin@example.com' ); my $SQL = "select url,filename from linktable"; my $sth = $dbh->prepare($SQL) or die "Prepare".$dbh->errstr; $sth-> execute() or die "".$dbh->errstr; while (my $row = $sth->fetchrow_arrayref) { my ($url,$filename)= ($row->[0],$row->[1]); print "\n$row[0] $row[1]\n"; my $resp = $ua->mirror( $url,$filename); if ( $resp->{success} ) { print "OK\n"; } else { print "Failure: $resp->{status}, $resp->{reason}\n"; } }

        no strict, no warnings, no creation of a database handle object, it's almost as though you've ignored everything 1nickt has provided in this thread...