in reply to Re: Runs from Command Line but NOT from Perl
in thread Runs from Command Line but NOT from Perl

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;

Replies are listed 'Best First'.
Re^3: Runs from Command Line but NOT from Perl
by poj (Abbot) on Oct 19, 2018 at 06:24 UTC
    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.