Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

curl2lwp - convert Curl command line arguments to LWP / Mechanize Perl code

by Corion (Pope)
on Nov 14, 2018 at 18:07 UTC ( #1225811=CUFP: print w/replies, xml ) Need Help??

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 (Bishop) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://1225811]
Approved by hippo
Front-paged by Discipulus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2019-10-19 02:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?