Documentation Packages Support

Http Server ⋅ Response

The Response class represents an HTTP response. The Response promise is returned by request handlers and middleware.


public function __construct(
    int $code = Status::OK,
    string[] | string[][] $headers = [],
    Amp\ByteStream\InputStream | string | null $stringOrStream = null,
    ?Trailers $trailers = null


int $code HTTP response status code
$headers An array of strings or an array of string arrays.
$stringOrStream Response body


Invokes dispose handlers (i.e. functions that registered via onDispose method).

Uncaught exceptions from the dispose handlers will be forwarded to the event loop error handler.

getBody(): Amp\ByteStream\InputStream

Returns the stream for the message body.

setBody(\Amp\ByteStream\InputStream | string | null $stringOrStream)

Sets the stream for the message body.

Using a string will automatically set the Content-Length header to the length of the given string. Setting an InputStream will remove the Content-Length header. If you know the exact content length of your stream, you can add a content-length header after calling setBody().

getHeaders(): string[][]

Returns the headers as a string-indexed array of arrays of strings or an empty array if no headers have been set.

hasHeader(string $name): bool

Checks if given header exists.

getHeaderArray(string $name): string[]

Returns the array of values for the given header or an empty array if the header does not exist.

getHeader(string $name): ?string

Returns the value of the given header. If multiple headers are present for the named header, only the first header value will be returned. Use getHeaderArray() to return an array of all values for the particular header. Returns null if the header does not exist.

setHeaders(array $headers): void

Sets the headers from the given array.

setHeader(string $name, string | string[] $value): void

Sets the header to the given value(s). All previous header lines with the given name will be replaced.

addHeader(string $name, string | string[] $value): void

Adds an additional header line with the given name.

removeHeader(string $name): void

Removes the given header if it exists. If multiple header lines with the same name exist, all of them are removed.

getStatus(): int

Returns the response status code.

getReason(): string

Returns the reason phrase describing the status code.

setStatus(int $code, string | null $reason): void

Sets the numeric HTTP status code (between 100 and 599) and reason phrase. Use null for the reason phrase to use the default phrase associated with the status code.

getCookies(): ResponseCookie[]

Returns all cookies in an associative map.

getCookie(string $name): ?ResponseCookie

Gets a cookie value by name or null if no cookie with that name is present.

Adds a cookie to the response.

removeCookie(string $name): void

Removes a cookie from the response.

getPushes(): Push[]

Returns list of push resources in an associative map:

    string $url => `Push`,

push(string $url, string[][] $headers): void

Indicate resources which a client likely needs to fetch. (e.g. Link: preload or HTTP/2 Server Push).

isUpgraded(): bool

Returns true if a detach callback has been set, false if none.

upgrade(callable $upgrade): void

Sets a callback to be invoked once the response has been written to the client and changes the status of the response to 101 Switching Protocols. The callback receives an instance of Driver\UpgradedSocket as the only argument. The callback may return a Generator which will be run as a coroutine. Throwing an exception from the callback disconnects the client and writes the exception message to the server log.

The callback may be removed by changing the status to something else.

getUpgradeCallable(): ?callable

Returns the upgrade function if present.

onDispose(callable $onDispose): void

Registers a function that is invoked when the Response is discarded. A response is discarded either once it has been written to the client or if it gets replaced in a middleware chain.

getTrailers(): Trailers

Allows access to the Trailers of a response.

setTrailers(Trailers $trailers): void

Assigns the Trailers object to be used in the response. Trailers are sent once the entire response body has been set to the client.