It has been some time since I last used curl2perl (via Corion's HTTP::Request::FromCurl) and I remember it was working perfectly. Now, v0.55, it seems counter-intuitive how the curl's command-line parameters are passed. For example, how to convert this curl command coming out of FF developer tools?:

curl 'https://example.com' -H 'User-Agent: Mozilla/5.0 (X11;)' -H 'Accept: application/json, text/plain, */*' -H 'Accept-Language: en-GB,en;q=0.5'

I tried:

curl2perl 'https://example.com' -H 'User-Agent: Mozilla/5.0 (X11;)' -H 'Accept: application/json, text/plain, */*' -H 'Accept-Language: en-GB,en;q=0.5'

I gives me usage, confused by -H. Ok then let's add all that follows the url into double quotes:

curl2perl 'https://example.com' "-H 'User-Agent: Mozilla/5.0 (X11;)' -H 'Accept: application/json, text/plain, */*' -H 'Accept-Language: en-GB,en;q=0.5'"

It thinks all headers are part of the first -H:

## Please see file perltidy.ERR my $ua = LWP::UserAgent->new('send_te' => '0'); my $r = HTTP::Request->new( 'GET' => 'https://example.com/', [ 'Accept' => '*/*', 'User-Agent' => 'curl/7.55.1', ''User-Agent' => 'Mozilla/5.0 (X11;)' -H 'Accept: applicat +ion/json, text/plain, */*' -H 'Accept-Language: en-GB,en;q=0.5'' ], ); my $res = $ua->request( $r, );

Changing quotes from single to double and vice-versa gives same result.

What sort of works is when each curl parameter is quoted:

curl2perl 'https://example.com' "-H 'User-Agent: Mozilla/5.0 (X11;)'" "-H 'Accept: application/json, text/plain, */*'" "-H 'Accept-Language: en-GB,en;q=0.5'"

which gives this, notice the extraneous single-quotes:

my $ua = LWP::UserAgent->new('send_te' => '0'); my $r = HTTP::Request->new( 'GET' => 'https://example.com/', [ 'Accept' => '*/*', 'User-Agent' => 'curl/7.55.1', ''Accept' => 'application/json, text/plain, */*'', ''Accept-Language' => 'en-GB,en;q=0.5'', ''User-Agent' => 'Mozilla/5.0 (X11;)'' ], );

But adding all these quotes is way too much work. Plus I am sure that I used it without having to quote anything. All I had to do was to remove the curl from the beginning of the command-line. Perhaps the Getopt::Long should be removed?

What DOES WORK is diy:

use HTTP::Request::FromCurl; print HTTP::Request::FromCurl->new(command_curl => <<'EOC')->as_snippe +t; curl 'http://example.com' --compressed -H 'User-Agent: myagent' EOC

(note: when omitting curl, it complains about Can't locate object method "host_port" via package "URI::_generic" ... HTTP/Request/CurlParameters.pm line 457., I mention it just in case this mis-usage reveals something more serious.)

bonus b[au]g: request2perl is missing a use Pod::Usage;.

I hope I am doing something wrong :) or else i write my own (that's a threat hehe :))


In reply to Converting a curl cli to perl with curl2perl gives me pain by bliako

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.