Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Curl usage with Perl's system command

by Loops (Curate)
on Oct 28, 2014 at 14:53 UTC ( [id://1105326]=note: print w/replies, xml ) Need Help??


in reply to Curl usage with Perl's system command

Hey there

You need to build up the parameter array manually, with each individual element, not all in one string. This way system will call curl directly, and not via the shell where the quoting gets complicated:

use strict; use warnings; use Capture::Tiny qw/ capture /; my ($to_id, $subject, $message) = qw(AA BB CC); my @args = ( '-v', '-s', '--user', 'api:key*****', '-F', "from=*****", '-F', "to=Name <$to_id>", '-F', "subject=$subject", '-F', "text=$message", 'https://api.****/**/*****/messages', ); my ($stdout,$stderr,$exit) = capture { system 'curl', @args; }; print $stdout,$stderr,$exit;

Added -v to the options so that you can see what is going on, although you wont want that once you get things sorted out. Another option is WWW::Curl on CPAN which has a Perl API overtop of Curl.

Replies are listed 'Best First'.
Re^2: Curl usage with Perl's system command
by perlron (Pilgrim) on Oct 28, 2014 at 15:02 UTC
    ok..will def try this out. this is what the other monk in the CRLF post i mentioned was trying to say, somehow i didnt grasp it. thanks
    Do not wait to strike when the iron is hot! Make it hot by striking - WB Yeats
Re^2: Curl usage with Perl's system command
by perlron (Pilgrim) on Oct 28, 2014 at 16:32 UTC
    partial success. Its difficult to correct errors inside strings.

    https://api.****/**/*****/messages has to be quoted with '\' and so does api:key***** without which i get a HTTP 400 bad request. found this by checking the original curl string from the company.
    when i use it with '' eg '\'api:key-123456etc1234\'' and '\'https://api.****/**/*****/messages\''
    i get this message which seems quite documented on stack overflow
    Protocol 'https not supported or disabled in libcurl
    same for http as well.strange
    Protocol 'http not supported or disabled in libcurl
    i will try this on the server, maybe it might just work there.
    1st update curl-config --protocols shows me that HTTP is supported on my m/c.
    so i guess im still in the woods with quoting things in perl :D. thanks for the help sir.
    ill find a way out.
    2nd update. thanks its working for me. well its just a string thing i goofed up somewhere as revealed in libcurl documentation.ive changed the order of the URL around. For all practical purposes your code was spot on. The only thing i faced is the to name<name@email> for sender address was not being accepted and i actually passed a string there.
    But this is just a start. I guess LWP or WWW::Curl will be the right approach i understand.thanks!
    for funssake im putting up the working code here. But Loops code is 99.99%% accurate for my use i would say. I hope such situatiosn never arise where i need to take someone elses code :D. Thanks
    use strict; use warnings; use Capture::Tiny qw/ capture /; my $message = "first line of text"; $message .= "\nsecond line of text"; my ($to_id, $subject) = qw(ron\@asdfasdf.cc testMessage); my @args = ( '-v', '-s', '--user',"api:key-123456", , 'https://api.XXX.net/v2/xxx.yyy.org/messages', ,'-F', "from='xxx yyy <postmaster\@xxx.yyy.org>", ,'-F', "to=ron\@xxx.cc", ,'-F', "subject=\'$subject\'", ,'-F', "text=\'$message\'"); my ($stdout,$stderr,$exit) = capture { system 'curl', @args; };

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2024-04-20 03:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found