NAME

Twilio::Http::Client - Base class for HTTP transports

SYNOPSIS

package My::Transport;
use parent 'Twilio::Http::Client';

sub send_request {
  my ($self, $request) = @_;

  # Send $request->{method} to $request->{url} with $request->{headers}, an
  # optional $request->{content} and a $request->{timeout} in seconds.

  return Twilio::Http::Response->new($status, $body, \%headers);
}

package main;
my $client = Twilio::Rest::Client->new($sid, $token, {
  http_client => My::Transport->new,
});

DESCRIPTION

This class turns a request into its exact wire format: URL, headers and body. A transport only has to deliver it, by implementing "send_request". This is how to replace the default Twilio::Http::TinyClient, for example to use a proxy library or a system without OpenSSL.

Text may be given as characters or as UTF-8 bytes in form and query values. A JSON body must hold characters.

METHODS

new

my $http = Twilio::Http::Client->new(\%options);

The timeout option sets the default timeout in seconds, which is 60.

request

my $response = $http->request($method, $url, \%options);

Prepares and sends a request, and returns a Twilio::Http::Response. The options are params (query string), data (request body), headers, username, password and timeout. An array reference as a value of params or data sends the key once per item, and undef values are left out.

The body is sent as a form unless the Content-Type header is application/json. When a value of the form, or an item of a list, is a Twilio::Http::File, the form is sent as multipart/form-data instead, with one part per value. The Authorization header is sent on the first request rather than after a 401 challenge.

send_request

my $response = $http->send_request($request);

Subclasses must implement this. It receives the hash reference built by "prepare_request", and must return a Twilio::Http::Response.

prepare_request

my $request = $http->prepare_request($method, $url, \%options);

Returns a hash reference with method, url, headers, timeout, and content for POST, PUT and PATCH.

build_query

my $query = $http->build_query({ To => '+15558675309', MediaUrl => [$first, $second] });

Returns the URL-encoded form of a hash reference, with keys in sorted order.

last_request

Returns the last request sent, as built by "prepare_request". It includes the Authorization header, so treat it as a secret.

last_response

Returns the last Twilio::Http::Response, or nothing if the last request failed before a response arrived.

LICENSE

Copyright (c) 2026 Francisco Zarabozo. Released under the MIT License.