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 | |
by bliako (Abbot) on Jan 24, 2026 at 12:28 UTC |