Name

SPVM::HTTP::Tiny::Response - HTTP Response Object

Description

The HTTP::Tiny::Response class in SPVM represents an HTTP response received by HTTP::Tiny.

This class provides an interface compatible with Perl's HTTP::Tiny response hash, while also allowing access to the underlying Mojo::Message::Response object.

Usage

my $res = $http->get($ctx, "http://example.com/");

# Check if the request was successful (status 2xx)
if ($res->success) {
  # Get status code and reason phrase
  my $status = $res->status;   # 200
  my $reason = $res->reason;   # "OK"

  # Get response headers as a Hash
  my $headers = $res->headers;
  my $type = (string)$headers->get("content-type");

  # Get response body
  my $content = $res->content;
  
  # Access underlying Mojo object for advanced features
  my $mojo_res = $res->res;
  my $version = $mojo_res->version;
}

Fields

tx

has tx : ro Mojo::Transaction::HTTP;

The underlying Mojo::Transaction::HTTP object. This field stores the entire HTTP transaction, including the request and response.

res

has res : virtual ro Mojo::Message::Response;

The underlying Mojo::Message::Response object. This is a virtual field that retrieves the response object directly from the "tx" field.

You can use this field to access low-level response data not provided by the HTTP::Tiny::Response wrapper.

Instance Methods

protocol

method protocol : string ();

Returns the HTTP protocol string, such as HTTP/1.1.

status

method status : int ();

Returns the HTTP status code (e.g., 200, 404).

success

method success : int ();

Returns 1 if the "status" is a 2xx code, 0 otherwise.

reason

method reason : string ();

Returns the HTTP status message (e.g., OK, Not Found).

headers

method headers : Hash ();

Returns the response headers as a Hash object. The keys are normalized to lower-case. Values are typically strings, but can be string arrays if a header has multiple values.

content

method content : string ();

Returns the response body as a string.

Copyright & License

Copyright (c) 2026 Yuki Kimoto

MIT License