1

I have a problem in send Restful request to HotelsPro web service.

Iam trying to send request to this link https://api-test.hotelspro.com:443 with basic credentials but I get error every time "Authentication credentials were not provided" although this credentials is working on browser.My code like follow

 sub getJSONdata {

    my ($SupplierXMLServer, $message, $compressed,$timeOut) = ();
       ($SupplierXMLServer, $message, $compressed,$timeOut) = @_;

    $SupplierXMLServer='https://api-test.hotelspro.com/api/v2/search/?destination_code=20b05&checkin=2016-11-09&checkout=2016-11-12&currency=USD&client_nationality=PS&pax=2';

    my $username = "Epilgrim";
    my $password = "xxxxxxxxxx";

    use LWP::UserAgent;
    my $userAgent = LWP::UserAgent->new(agent =>"1");
       $userAgent->credentials('https://api-test.hotelspro.com:443', 'api', $username , $password);
       $userAgent->timeout($timeOut) if($timeOut);  # in seconds
    use HTTP::Request::Common;
    my $response = '';
    if($compressed){
         $response = $userAgent->request( GET $SupplierXMLServer,
                                          Content_Type => 'application/json',
                                          Accept_Encoding => "gzip,deflate",
                                          Content => $message);
    }
    else{
         $response = $userAgent->request( GET $SupplierXMLServer,
                                          Content_Type => 'application/json',
                                          Content => $message);
    }
    return $response->error_as_HTML unless $response->is_success;
      #return $response->content;

    if($compressed){
    return $response->decoded_content;
  }

    else{
        return $response->content;
      }
    }

Please help me to write the correct code and send the request in correct way to get valid response.

  • In your call to `credentials`, can you try removing the protocol - it should be of the format `hostname:port` - i.e. `'api-test.hotelspro.com:443'` See http://stackoverflow.com/questions/1799147/why-dont-my-lwpuseragent-credentials-work – ardavey Oct 31 '16 at 17:27

1 Answers1

1

The given link redirects to https://api-test.hotelspro.com/login/?next=/, which is looking a Form Based Authentication page. But in your Perl script, you are trying Basic Authentication. Please check google to understand the difference between Basic Auth vs Form Based Auth.

Now, in order to perform Form Based Auth, its better to use WWW::Mechanize which is a wrapper around LWP but provide more convenient methods. Following is a sample code for Form Based Auth right from WWW::Mechanize's official help page:

 #!/usr/bin/perl -w -T

use strict;
use WWW::Mechanize;

my $login    = "login_name";
my $password = "password";
my $folder   = "folder";

my $url = "http://img78.photobucket.com/albums/v281/$login/$folder/";

# login to your photobucket.com account
my $mech = WWW::Mechanize->new();
$mech->get($url);
$mech->submit_form(
    form_number => 1,
    fields      => { password => $password },
);
die unless ($mech->success);

# upload image files specified on command line
foreach (@ARGV) {
    print "$_\n";
    $mech->form_number(2);
    $mech->field('the_file[]' => $_);
    $mech->submit();
}

Hope this helps!

J Singh
  • 162
  • 5