I'm fairly new to Perl. (using Perl 5) There's a requirement to call a REST API endpoint which expects a POST request and it only supports Basic auth.
I tried below logic but I'm getting: Can't call method "clone" on unblessed reference at C:/Strawberry/perl/vendor/lib/HTTP/Message.pm
use strict;
use warnings;
use Encode qw(encode_utf8);
use HTTP::Request ();
use JSON::MaybeXS qw(encode_json);
#use REST::Client;
use MIME::Base64;
my $username = '';
my $password = '';
my $url = 'https://testapi.com/webapi/AddData';
#my $header = ['Content-Type' => 'application/json; charset=UTF-8'];
my $header = {Accept => 'application/json', Authorization => 'Basic ' . encode_base64($username . ':' . $password)};
my $data = {GroupName=> 'City', Member=> 'New York'};
my $encoded_data = encode_utf8(encode_json($data));
my $r = HTTP::Request->new('POST', $url, $header, $encoded_data);
print $r->responseContent();
I may be completely wrong here with this logic but how can we correct or construct a proper Basic auth POST REST API call using PERL.
The API in question accepts data in following format:
{
"GroupName": "City",
"Member": "New York"
}
and if the call is succeded it returns the following data of which we need the "ReturnValue" data:
{
"IsPass": true,
"ReturnValue": "New city has been added"
}
EDIT
Got this working by tweaking the header logic:
my $header = ['Content-Type' => 'application/json; charset=UTF-8', 'Authorization' => 'Basic ' . encode_base64($username . ':' . $password)];