Perl
Chilkat2 Perl
Get a REST Response Header Value by Index
See more REST Examples
Demonstrates Rest.ResponseHdrValue, which returns the value of the response header at a zero-based index. The same index used with ResponseHdrName gives the corresponding field name.
Background. Response headers can be enumerated by index from 0 through NumResponseHeaders minus one, after ReadResponseHeader has been called.
Chilkat Perl Downloads
use strict;
use warnings;
use Chilkat2;
my $rest = Chilkat2::Rest->new;
my $bTls = 1;
my $bAutoReconnect = 1;
my $success = $rest->Connect('example.com', 443, $bTls, $bAutoReconnect);
if (!$success) {
print $rest->LastErrorText, "\n";
exit;
}
$success = $rest->SendReqNoBody('GET', '/api/items/123');
if (!$success) {
print $rest->LastErrorText, "\n";
exit;
}
my $statusCode = $rest->ReadResponseHeader();
if ($statusCode < 0) {
print $rest->LastErrorText, "\n";
exit;
}
# Return the value of the response header at a zero-based index. Use the same index with
# ResponseHdrName to get the corresponding field name.
my $numHeaders = $rest->NumResponseHeaders;
for my $i (0 .. $numHeaders - 1) {
my $value = $rest->ResponseHdrValue($i);
if (!$rest->LastMethodSuccess) {
print $rest->LastErrorText, "\n";
exit;
}
print 'Header ', $i, ' value: ', $value, "\n";
}