Perl
Chilkat2 Perl
Get a REST Response Header by Name
See more REST Examples
Demonstrates Rest.ResponseHdrByName, which returns the value of a response header field by name. Header field names are case-insensitive.
Background. Response headers are available after ReadResponseHeader has read the status line and headers. Lookup by name is convenient when a specific header, such as Content-Type, is needed.
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 a response header field by name. Header field names are case-insensitive.
my $contentType = $rest->ResponseHdrByName('Content-Type');
if (!$rest->LastMethodSuccess) {
print $rest->LastErrorText, "\n";
exit;
}
print 'Content-Type: ', $contentType, "\n";