Perl
Chilkat2 Perl
Send a REST Request using StringBuilder Bodies
See more REST Examples
Demonstrates Rest.FullRequestSb, which sends a complete request whose text body is read from a StringBuilder and stores the text response body in a second StringBuilder.
Background. Using StringBuilder objects for the request and response bodies avoids extra string conversions, which is helpful when working with large payloads.
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;
}
# FullRequestSb sends a request whose text body is read from a StringBuilder and stores the text
# response body in a second StringBuilder. This avoids extra string conversions for large bodies.
my $sbRequest = Chilkat2::StringBuilder->new;
$sbRequest->Append('{ "query": "chilkat" }');
$rest->AddHeader('Content-Type', 'application/json');
my $sbResponse = Chilkat2::StringBuilder->new;
$success = $rest->FullRequestSb('POST', '/api/search', $sbRequest, $sbResponse);
if (!$success) {
print $rest->LastErrorText, "\n";
exit;
}
my $responseText = $sbResponse->GetAsString();
if (!$sbResponse->LastMethodSuccess) {
print $sbResponse->LastErrorText, "\n";
exit;
}
print $responseText, "\n";