Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Converting a curl cli to perl with curl2perl gives me pain

by bliako (Abbot)
on Jan 23, 2026 at 18:43 UTC ( [id://11167217]=perlquestion: print w/replies, xml ) Need Help??

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

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 :))

Replies are listed 'Best First'.
Re: Converting a curl cli to perl with curl2perl gives me pain
by Corion (Patriarch) on Jan 23, 2026 at 20:37 UTC

    This sounds to me more like an issue with your shell and less with curl2perl. Using the website ,your command gets converted properly.

    If you're on Windows, I guess you need to convert all single quotes to double quotes.

      I am using recent bash in linux

      Anyway, I found the culprit: Getopt::Long's unhealthy obsession with case-insensitive command-line switches. curl's -H is confused to be your script's -h. Hence the usage message and exit.

      Solution: add 'no_ignore_case': Getopt::Long::Configure('pass_through', 'no_ignore_case');

      really useful script thanks

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11167217]
Approved by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2026-02-09 01:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.