I'm trying to convert a curl API to Perl. I reached for Corion's HTTP::Request::FromCurl because I've used it successfully several times in the past.

The problem is with the -d option followed by a JSON string. For example, see Geneea API / Basic Calls:

curl -X POST https://api.geneea.com/v3/analysis \ -H 'Authorization: user_key <your user key>' \ -H 'Content-Type: application/json' \ -d '{"text": "The pizza in London was great!"}'

My code:

#!/usr/bin/perl use warnings; use strict; my $user_key = '...'; use HTTP::Request::FromCurl; use LWP::UserAgent; my $req = 'HTTP::Request::FromCurl'->new(argv => [ -X => POST => 'https://api.geneea.com/v3/analysis', -H => "Authorization: user_key $user_key", -H => 'Content-Type: application/json', -d => '{"text": "The pizza in London was great!"}', ]); my $ua = 'LWP::UserAgent'->new; my $response = $ua->request($req->as_request); use Data::Dumper; print Dumper($response);
But the response is a 400 containing an error message wrapping a stack trace that says among other things
JsonParseException: Unexpected character (\'\'\' (code 39))

Interestingly, if I build the request from the $req->as_snippet, it works correctly. Dumping the HTTP::Request objects created by the two methods shows one difference: the as_request method adds single quotes to the content:

'_content' => '\'{"text": "The pizza in London was great!"}\'',

I identified the part of the code responsible for the quotes: it's the line 93 in CurlParameters.pm:

return sprintf "'%s'", $body

When I remove the single quotes (or just return $body), it starts to work. Why are the single quotes needed here? What am I doing wrong?

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

In reply to Curl and Quoting JSON Data by choroba

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.