After inspiration by Your Mother and some slight hacking, I'm very proud to announce HTTP::Request::FromCurl, together with its companion online site at https://corion.net/curl2lwp.psgi. The module and included curl2lwp program allow you to easily convert curl command lines to Perl code that uses LWP::UserAgent resp. WWW::Mechanize.

Some code:

curl2lwp.pl -X GET -A xpcurl/1.0 https://perlmonks.org/

outputs the following code:

my $ua = WWW::Mechanize->new(); my $r = HTTP::Request->new( 'GET' => 'https://perlmonks.org/', [ 'Accept' => '*/*', 'Host' => 'perlmonks.org:443', 'User-Agent' => 'curl/1.0', ], ); my $res = $ua->request( $r, );

The online version creates a bit more code, as the output there is likely not consumed by advanced Perl programmers.

The module parses a subset of the valid curl command lines and generates equivalent code for LWP::UserAgent for it. Support for other HTTP user agents (Mojo::UserAgent, AnyEvent::HTTP, HTTP::Future) is not yet implemented but I welcome contributions there.

The app driving the online interface is not yet released onto CPAN, but as it is mostly HTML scaffolding and some Javascript, it should be released fairly soon.

Replies are listed 'Best First'.
Re: curl2lwp - convert Curl command line arguments to LWP / Mechanize Perl code
by Your Mother (Archbishop) on Nov 14, 2018 at 18:27 UTC

    So cool. You are awesome. Exactly what I was hoping existed and pow, now it does. :P

    For others to consider, the use case I will generally have looks like–

    • Open dev tools in browser.
    • Perform action.
    • Network ⇾ Right click on relevant entry ⇾ Copy ⇾ Copy as cURL.
    • Run it through this new code.
    • Add the code to either a test or put it under watch or something to execute while editing code or watching logs.