Home > Software design >  Using PERL to perform POST request to a REST API using Basic auth
Using PERL to perform POST request to a REST API using Basic auth

Time:10-13

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)];

CodePudding user response:

You haven't actually sent the request. You just constructed it. As the documentation says:

my $r = HTTP::Request->new('POST', $url, $header, $encoded_data);
# at this point, we could send it via LWP::UserAgent
# my $ua = LWP::UserAgent->new();
# my $res = $ua->request($r);

Also, responseContent doesn't exist in HTTP::Request nor HTTP::Response (or HTTP::Whatever). It exists in REST::Client, though, which you commented out.

CodePudding user response:

Try this, look at my coments mark as [Welcho]

use warnings;
use Encode qw(encode_utf8);
use HTTP::Request ();
use JSON::MaybeXS qw(encode_json);
#use REST::Client;
use MIME::Base64;
use LWP::UserAgent; # [Welcho] Add this

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);

# [Welcho] comment this print $r->responseContent();

#[Welcho]
my $ua = LWP::UserAgent->new();
my $res = $ua->request($r);
print $res->decoded_content;
  • Related