in reply to Runs from Command Line but NOT from Perl

why don't you use LWP instead of shelling out to curl?

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

  • Comment on Re: Runs from Command Line but NOT from Perl

Replies are listed 'Best First'.
Re^2: Runs from Command Line but NOT from Perl
by Your Mother (Archbishop) on Oct 18, 2018 at 17:31 UTC

    Tangent: Has anyone ever written a curl2lwp kind of thing in the vein of s2p and a2p? It would obviously be more verbose, especially in output, but I just realized that I'd like to have it so I can copy curl commands from the dev panel and such and use them to stub out a new LPW/Mech script.

    Update: curl2perl really rolls off the tongue…

      You can write curl2perl

        As is often the case, I almost certainly bit off more than I can chew but…

        my $ok = GetOptions( "abstract-unix-socket" => \my $path, "anyauth" => \my $anyauth, "a|append" => \my $append, "basic" => \my $basic, "cacert" => \my $cacert, "capath" => \my $capath, "cert-status" => \my $cert_status, # ...

        …there is a 200 line stub.

Re^2: Runs from Command Line but NOT from Perl
by Anonymous Monk on Oct 19, 2018 at 02:23 UTC
    I did try with LWP but I get the same perplexing problem!!! I'm trying to go to https://taxfoundation.org/2018-tax-reform-calculator/ and use their tax calculator from Perl. I am TOTALLY bamboozakled on why this is failing!
    #!/usr/bin/perl use strict; use warnings; use HTTP::Request::Common qw( POST ); use LWP::UserAgent; my $ua = LWP::UserAgent->new; my $req = POST 'https://tax-calculator-api.taxfoundation.org/taxcalc/t +cja_submit', [ Host => 'tax-calculator-api.taxfoundation.org', Connection => 'keep-alive', Origin => 'https://taxfoundation.org', 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64 +) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/ +537.36', 'content-type' => 'application/json', Accept => '*/*', Referer => 'https://taxfoundation.org/2018-tax-refor +m-calculator/', 'Accept-Encoding' => 'gzip, deflate, br', 'Accept-Language' => 'en-US,en;q=0.9', Content => [ '401k_contributions' => 0, business_income => 0, charity_contributions => 0, child_dep => 2, filing_status => 2, interest_paid => 0, medical_expenses => 0, nonchild_dep => 0, ordinary_income1 => 150000, ordinary_income2 => 0, other_itemized => 0, qualified_income => 0, sl_income_tax => 0, sl_property_tax => 0, ss_income => 0, ], ]; print $ua->request($req)->as_string;
      why this is failing

      Because content-type' => 'application/json' you have to json encode the content

      #!/usr/bin/perl use strict; use warnings; use HTTP::Request::Common qw( POST ); use LWP::UserAgent; use Encode qw(encode_utf8); use JSON::MaybeXS qw(encode_json); my $ua = LWP::UserAgent->new; my $data = { '401k_contributions' => 0, business_income => 0, charity_contributions => 0, child_dep => 2, filing_status => 2, interest_paid => 0, medical_expenses => 0, nonchild_dep => 0, ordinary_income1 => 150000, ordinary_income2 => 0, other_itemized => 0, qualified_income => 0, sl_income_tax => 0, sl_property_tax => 0, ss_income => 0, }; my $content = encode_utf8(encode_json($data)); my $req = POST 'https://tax-calculator-api.taxfoundation.org/taxcalc/t +cja_submit', Host => 'tax-calculator-api.taxfoundation.org', Connection => 'keep-alive', Origin => 'https://taxfoundation.org', 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64 +) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/ +537.36', 'content-type' => 'application/json', Accept => '*/*', Referer => 'https://taxfoundation.org/2018-tax-refor +m-calculator/', 'Accept-Encoding' => 'gzip, deflate, br', 'Accept-Language' => 'en-US,en;q=0.9', Content => $content ; #print $req->as_string; print $ua->request($req)->as_string;
      poj
        my $content = encode_utf8(encode_json($data));
        Note: at least encode_json of JSON::PP produces a byte-string already encoded to UTF-8, so encoding it once more will lead to bugs the moment you encounter a non-ASCII character in the document.