Title: Response
Published: May 20, 2026

---

# class Response {}

## In this article

 * [Description](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-response/?output_format=md#description)
 * [Methods](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-response/?output_format=md#methods)
 * [Source](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-response/?output_format=md#source)
 * [Changelog](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-response/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-response/?output_format=md#wp--skip-link--target)

Represents an HTTP response.

## 󠀁[Description](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-response/?output_format=md#description)󠁿

This class encapsulates HTTP response data that has been converted from PSR-7 responses
by the HTTP transporter.

## 󠀁[Methods](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-response/?output_format=md#methods)󠁿

| Name | Description | 
| [Response::__clone](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-response/__clone/) | Creates a deep clone of this response. | 
| [Response::__construct](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-response/__construct/) | Constructor. | 
| [Response::fromArray](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-response/fromarray/) | {@inheritDoc} | 
| [Response::getBody](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-response/getbody/) | Gets the response body. | 
| [Response::getData](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-response/getdata/) | Gets the response data as an array. | 
| [Response::getHeader](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-response/getheader/) | Gets a specific header value. | 
| [Response::getHeaderAsString](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-response/getheaderasstring/) | Gets header values as a comma-separated string. | 
| [Response::getHeaders](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-response/getheaders/) | Gets the response headers. | 
| [Response::getJsonSchema](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-response/getjsonschema/) | {@inheritDoc} | 
| [Response::getStatusCode](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-response/getstatuscode/) | Gets the HTTP status code. | 
| [Response::hasHeader](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-response/hasheader/) | Checks if the response has a header. | 
| [Response::isSuccessful](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-response/issuccessful/) | Checks if the response indicates success. | 
| [Response::toArray](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-response/toarray/) | {@inheritDoc} |

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-response/?output_format=md#source)󠁿

    ```php
    class Response extends AbstractDataTransferObject
    {
        public const KEY_STATUS_CODE = 'statusCode';
        public const KEY_HEADERS = 'headers';
        public const KEY_BODY = 'body';
        /**
         * @var int The HTTP status code.
         */
        protected int $statusCode;
        /**
         * @var HeadersCollection The response headers.
         */
        protected HeadersCollection $headers;
        /**
         * @var string|null The response body.
         */
        protected ?string $body;
        /**
         * Constructor.
         *
         * @since 0.1.0
         *
         * @param int $statusCode The HTTP status code.
         * @param array<string, string|list<string>> $headers The response headers.
         * @param string|null $body The response body.
         *
         * @throws InvalidArgumentException If the status code is invalid.
         */
        public function __construct(int $statusCode, array $headers, ?string $body = null)
        {
            if ($statusCode < 100 || $statusCode >= 600) {
                throw new InvalidArgumentException('Invalid HTTP status code: ' . $statusCode);
            }
            $this->statusCode = $statusCode;
            $this->headers = new HeadersCollection($headers);
            $this->body = $body;
        }
        /**
         * Creates a deep clone of this response.
         *
         * Clones the headers collection to ensure the cloned
         * response is independent of the original.
         *
         * @since 0.4.2
         */
        public function __clone()
        {
            // Clone headers collection
            $this->headers = clone $this->headers;
        }
        /**
         * Gets the HTTP status code.
         *
         * @since 0.1.0
         *
         * @return int The status code.
         */
        public function getStatusCode(): int
        {
            return $this->statusCode;
        }
        /**
         * Gets the response headers.
         *
         * @since 0.1.0
         *
         * @return array<string, list<string>> The headers.
         */
        public function getHeaders(): array
        {
            return $this->headers->getAll();
        }
        /**
         * Gets a specific header value.
         *
         * @since 0.1.0
         *
         * @param string $name The header name (case-insensitive).
         * @return list<string>|null The header value(s) or null if not found.
         */
        public function getHeader(string $name): ?array
        {
            return $this->headers->get($name);
        }
        /**
         * Gets header values as a comma-separated string.
         *
         * @since 0.1.0
         *
         * @param string $name The header name (case-insensitive).
         * @return string|null The header values as a comma-separated string or null if not found.
         */
        public function getHeaderAsString(string $name): ?string
        {
            return $this->headers->getAsString($name);
        }
        /**
         * Gets the response body.
         *
         * @since 0.1.0
         *
         * @return string|null The body.
         */
        public function getBody(): ?string
        {
            return $this->body;
        }
        /**
         * Checks if the response has a header.
         *
         * @since 0.1.0
         *
         * @param string $name The header name.
         * @return bool True if the header exists, false otherwise.
         */
        public function hasHeader(string $name): bool
        {
            return $this->headers->has($name);
        }
        /**
         * Checks if the response indicates success.
         *
         * @since 0.1.0
         *
         * @return bool True if status code is 2xx, false otherwise.
         */
        public function isSuccessful(): bool
        {
            return $this->statusCode >= 200 && $this->statusCode < 300;
        }
        /**
         * Gets the response data as an array.
         *
         * Attempts to decode the body as JSON. Returns null if the body
         * is empty or not valid JSON.
         *
         * @since 0.1.0
         *
         * @return array<string, mixed>|null The decoded data or null.
         */
        public function getData(): ?array
        {
            if ($this->body === null || $this->body === '') {
                return null;
            }
            $data = json_decode($this->body, \true);
            if (json_last_error() !== \JSON_ERROR_NONE) {
                return null;
            }
            /** @var array<string, mixed>|null $data */
            return is_array($data) ? $data : null;
        }
        /**
         * {@inheritDoc}
         *
         * @since 0.1.0
         */
        public static function getJsonSchema(): array
        {
            return ['type' => 'object', 'properties' => [self::KEY_STATUS_CODE => ['type' => 'integer', 'minimum' => 100, 'maximum' => 599, 'description' => 'The HTTP status code.'], self::KEY_HEADERS => ['type' => 'object', 'additionalProperties' => ['type' => 'array', 'items' => ['type' => 'string']], 'description' => 'The response headers.'], self::KEY_BODY => ['type' => ['string', 'null'], 'description' => 'The response body.']], 'required' => [self::KEY_STATUS_CODE, self::KEY_HEADERS]];
        }
        /**
         * {@inheritDoc}
         *
         * @since 0.1.0
         *
         * @return ResponseArrayShape
         */
        public function toArray(): array
        {
            $data = [self::KEY_STATUS_CODE => $this->statusCode, self::KEY_HEADERS => $this->headers->getAll()];
            if ($this->body !== null) {
                $data[self::KEY_BODY] = $this->body;
            }
            return $data;
        }
        /**
         * {@inheritDoc}
         *
         * @since 0.1.0
         */
        public static function fromArray(array $array): self
        {
            static::validateFromArrayData($array, [self::KEY_STATUS_CODE, self::KEY_HEADERS]);
            return new self($array[self::KEY_STATUS_CODE], $array[self::KEY_HEADERS], $array[self::KEY_BODY] ?? null);
        }
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/php-ai-client/src/providers/http/dto/response.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-includes/php-ai-client/src/Providers/Http/DTO/Response.php#L25)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/php-ai-client/src/Providers/Http/DTO/Response.php#L25-L211)

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wordpress-aiclient-providers-http-dto-response/?output_format=md#changelog)󠁿

| Version | Description | 
| [0.1.0](https://developer.wordpress.org/reference/since/0.1.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwordpress-aiclient-providers-http-dto-response%2F)
before being able to contribute a note or feedback.