user3906716 user3906716 - 1 year ago 280
Perl Question

Implementing custom Curl authentication in Perl script

I am building bot application trying to use perl script to implement the curl request, the issue I have is with Authorization.

The simple curl command is something like.

curl \

I preferred not to use system() calls from the Perl script as there will be plenty of back and forth between the user and the bot.

I found this library

I was searching for setopt function in order to find out which params does it accept as my issue is where to put the Authorization param inside the command.
I found this link

My Script code for now it is like the following:

use strict;
use warnings;
use WWW::Curl::Easy;

my $curl = WWW::Curl::Easy->new;
my $Authorization="Authorization: Bearer VM2CKBMXI3AVX2GMYPLBMYFRW3RCHYXS";

$curl->setopt(CURLOPT_URL, '');

# A filehandle, reference to a scalar or reference to a typeglob can be used here.
my $response_body;

# Starts the actual request
my $retcode = $curl->perform;

# Looking at the results...
if ($retcode == 0) {
print("Transfer went ok\n");
my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
# judge result and next action based on $response_code
print("Received response: $response_body\n");
} else {
# Error code, type of error, error message
print("An error happened: $retcode ".$curl->strerror($retcode)." ".$curl->errbuf."\n");

I just need to know which CURLOPT should I need to use in order to implement authorization.
If you have any idea that will be great.

Eran Gross

Answer Source

If you just want to set the header "Authorization" the option is CURLOPT_HTTPHEADER:

use strict;
use warnings;
use WWW::Curl::Easy;
my $curl = WWW::Curl::Easy->new;
$curl->setopt(CURLOPT_URL, '');
my @headers  = ("Authorization: keygoeshere", "X-Foo: Bah");
$curl->setopt(CURLOPT_HTTPHEADER, \@headers);
$curl->setopt(CURLOPT_HEADER, 1);
my $retcode = $curl->perform;


GET dumprequest.php HTTP/1.1
Accept: */*
Authorization: keygoeshere
X-Foo: Bah

But if you actually want to do HTTP authentication then you would use CURLOPT_USERPWD and CURLOPT_HTTPAUTH. See for more info on options.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download