Class Request
Implements methods for HTTP requests.
Used by Cake\Http\Client to contain request information for making requests.
Constants
-
stringMETHOD_DELETE ¶'DELETE'HTTP DELETE method
-
stringMETHOD_GET ¶'GET'HTTP GET method
-
stringMETHOD_HEAD ¶'HEAD'HTTP HEAD method
-
stringMETHOD_OPTIONS ¶'OPTIONS'HTTP OPTIONS method
-
stringMETHOD_PATCH ¶'PATCH'HTTP PATCH method
-
stringMETHOD_POST ¶'POST'HTTP POST method
-
stringMETHOD_PUT ¶'PUT'HTTP PUT method
-
stringMETHOD_TRACE ¶'TRACE'HTTP TRACE method
-
intSTATUS_ACCEPTED ¶202HTTP 202 code
-
intSTATUS_CREATED ¶201HTTP 201 code
-
intSTATUS_FOUND ¶302HTTP 302 code
-
intSTATUS_MOVED_PERMANENTLY ¶301HTTP 301 code
-
intSTATUS_NON_AUTHORITATIVE_INFORMATION ¶203HTTP 203 code
-
intSTATUS_NO_CONTENT ¶204HTTP 204 code
-
intSTATUS_OK ¶200HTTP 200 code
-
intSTATUS_PERMANENT_REDIRECT ¶308HTTP 308 code
-
intSTATUS_SEE_OTHER ¶303HTTP 303 code
-
intSTATUS_TEMPORARY_REDIRECT ¶307HTTP 307 code
Property Summary
-
$headerNames protected
array<non-empty-string, non-empty-string>Map of lowercased header name to the original name used to register the header.
-
$headers protected
array<non-empty-string, non-empty-list<string>>List of all registered headers, as original name => list of values.
-
$method protected
stringThe HTTP method.
-
$protocol protected
stringThe HTTP protocol version.
-
$requestTarget protected
string|nullThe request target, if it has been explicitly set.
-
$stream protected
Psr\Http\Message\StreamInterfaceThe message body.
-
$uri protected
Psr\Http\Message\UriInterfaceThe request URI.
Method Summary
-
__construct() public
Constructor
Provides backwards compatible defaults for some properties.
-
addHeaders() protected
Add an array of headers to the request.
-
assertHeaderName() protected
Validate a header name.
-
assertStatus() protected
Validate status line components before they reach an emitter.
-
filterHeaderValue() protected
Validate and normalize header values.
-
getBody() public
Gets the body of the message.
-
getHeader() public
Retrieves a message header value by the given case-insensitive name.
-
getHeaderLine() public
Retrieves a comma-separated string of the values for a single header.
-
getHeaders() public
Retrieves all message headers.
-
getHostHeader() protected
Build a Host header, using IDNA for HTTP hostnames.
-
getMethod() public
Retrieves the HTTP method of the request.
-
getProtocolVersion() public
Retrieves the HTTP protocol version as a string.
-
getRequestTarget() public
Retrieves the message's request target.
Retrieves the message's request-target either as it will appear (for clients), as it appeared at request (for servers), or as it was specified for the instance (see withRequestTarget()).
In most cases, this will be the origin-form of the composed URI, unless a value was provided to the concrete implementation (see withRequestTarget() below).
If no URI is available, and no request-target has been specifically provided, this method MUST return the string "/".
-
getUri() public
Retrieves the URI instance.
This method MUST return a UriInterface instance.
-
hasHeader() public
Checks if a header exists by the given case-insensitive name.
-
removeHeader() protected
Remove a header from the current instance.
-
replaceHeader() protected
Replace a header on the current instance.
-
setContent() protected
Set the body/payload for the message.
Array data will be serialized with {@link \Cake\Http\FormData}, and the content-type will be set.
-
setMethod() protected
Set and validate the HTTP method.
-
withAddedHeader() public
Return an instance with the specified header appended with the given value.
-
withBody() public
Return an instance with the specified message body.
-
withHeader() public
Return an instance with the provided header, replacing any existing values of any headers with the same case-insensitive name.
-
withMethod() public
Return an instance with the provided HTTP method.
While HTTP method names are typically all uppercase characters, HTTP method names are case-sensitive and thus implementations SHOULD NOT modify the given string.
This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that has the changed request method.
-
withProtocolVersion() public
Return an instance with the specified HTTP protocol version.
-
withRequestTarget() public
Return an instance with the specific request-target.
If the request needs a non-origin-form request-target — e.g., for specifying an absolute-form, authority-form, or asterisk-form — this method may be used to create an instance with the specified request-target, verbatim.
This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that has the changed request target.
-
withUri() public
Returns an instance with the provided URI.
This method MUST update the Host header of the returned request by default if the URI contains a host component. If the URI does not contain a host component, any pre-existing Host header MUST be carried over to the returned request.
You can opt-in to preserving the original state of the Host header by setting
$preserveHosttotrue. When$preserveHostis set totrue, this method interacts with the Host header in the following ways:- If the Host header is missing or empty, and the new URI contains a host component, this method MUST update the Host header in the returned request.
- If the Host header is missing or empty, and the new URI does not contain a host component, this method MUST NOT update the Host header in the returned request.
- If a Host header is present and non-empty, this method MUST NOT update the Host header in the returned request.
This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that has the new UriInterface instance.
-
withoutHeader() public
Return an instance without the specified header.
Method Detail
__construct() ¶ public
__construct(Psr\Http\Message\UriInterface|string $url = '', string $method = self::METHOD_GET, array<non-empty-string, string|array<string>> $headers = [], array|string|null $data = null)
Constructor
Provides backwards compatible defaults for some properties.
Parameters
-
Psr\Http\Message\UriInterface|string$url optional The request URL
-
string$method optional The HTTP method to use.
-
array<non-empty-string, string|array<string>>$headers optional The HTTP headers to set.
-
array|string|null$data optional The request body to use.
addHeaders() ¶ protected
addHeaders(array<non-empty-string, string|array<string>> $headers): void
Add an array of headers to the request.
Parameters
-
array<non-empty-string, string|array<string>>$headers The headers to add.
Returns
voidassertHeaderName() ¶ protected
assertHeaderName(string $name): void
Validate a header name.
Parameters
-
string$name The header name.
Returns
voidThrows
InvalidArgumentExceptionWhen the name isn't a valid RFC 7230 token.
assertStatus() ¶ protected
assertStatus(int $code, string $reasonPhrase): void
Validate status line components before they reach an emitter.
Parameters
-
int$code HTTP status code.
-
string$reasonPhrase Optional reason phrase.
Returns
voidfilterHeaderValue() ¶ protected
filterHeaderValue(mixed $values): non-empty-list<string>
Validate and normalize header values.
Parameters
-
mixed$values A header value or list of header values.
Returns
non-empty-list<string>Throws
InvalidArgumentExceptionWhen a value is invalid.
getBody() ¶ public
getBody(): Psr\Http\Message\StreamInterface
Gets the body of the message.
Returns
Psr\Http\Message\StreamInterfacegetHeader() ¶ public
getHeader(string $name): list<string>
Retrieves a message header value by the given case-insensitive name.
Parameters
-
string$name Case-insensitive header name.
Returns
list<string>The header values, or an empty array if the header is not set.
getHeaderLine() ¶ public
getHeaderLine(string $name): string
Retrieves a comma-separated string of the values for a single header.
Parameters
-
string$name Case-insensitive header name.
Returns
stringThe header values joined by a comma, or an empty string if the header is not set.
getHeaders() ¶ public
getHeaders(): array<non-empty-string, non-empty-list<string>>
Retrieves all message headers.
Returns
array<non-empty-string, non-empty-list<string>>Header names mapped to their list of values.
getHostHeader() ¶ protected
getHostHeader(Psr\Http\Message\UriInterface $uri): string
Build a Host header, using IDNA for HTTP hostnames.
Parameters
-
Psr\Http\Message\UriInterface$uri Request URI.
Returns
stringgetProtocolVersion() ¶ public
getProtocolVersion(): string
Retrieves the HTTP protocol version as a string.
Returns
stringHTTP protocol version.
getRequestTarget() ¶ public
getRequestTarget(): string
Retrieves the message's request target.
Retrieves the message's request-target either as it will appear (for clients), as it appeared at request (for servers), or as it was specified for the instance (see withRequestTarget()).
In most cases, this will be the origin-form of the composed URI, unless a value was provided to the concrete implementation (see withRequestTarget() below).
If no URI is available, and no request-target has been specifically provided, this method MUST return the string "/".
Returns
stringgetUri() ¶ public
getUri(): UriInterface
Retrieves the URI instance.
This method MUST return a UriInterface instance.
Returns
UriInterfacehasHeader() ¶ public
hasHeader(string $name): bool
Checks if a header exists by the given case-insensitive name.
Parameters
-
string$name Case-insensitive header name.
Returns
boolremoveHeader() ¶ protected
removeHeader(string $name): void
Remove a header from the current instance.
Parameters
-
string$name Case-insensitive header name.
Returns
voidreplaceHeader() ¶ protected
replaceHeader(non-empty-string $name, non-empty-list<string> $value): void
Replace a header on the current instance.
Parameters
-
non-empty-string$name The header name.
-
non-empty-list<string>$value The filtered header values.
Returns
voidsetContent() ¶ protected
setContent(array|string $content): $this
Set the body/payload for the message.
Array data will be serialized with {@link \Cake\Http\FormData}, and the content-type will be set.
Parameters
-
array|string$content The body for the request.
Returns
$thissetMethod() ¶ protected
setMethod(string $method): void
Set and validate the HTTP method.
Parameters
-
string$method The HTTP method.
Returns
voidThrows
InvalidArgumentExceptionWhen the method is not a valid token.
withAddedHeader() ¶ public
withAddedHeader(string $name, string|string[] $value): static
Return an instance with the specified header appended with the given value.
Parameters
-
string$name Case-insensitive header name.
-
string|string[]$value Header value(s).
Returns
staticThrows
InvalidArgumentExceptionFor invalid header names or values.
withBody() ¶ public
withBody(StreamInterface $body): static
Return an instance with the specified message body.
Parameters
-
StreamInterface$body The new body.
Returns
staticwithHeader() ¶ public
withHeader(string $name, string|string[] $value): static
Return an instance with the provided header, replacing any existing values of any headers with the same case-insensitive name.
Parameters
-
string$name Case-insensitive header name.
-
string|string[]$value Header value(s).
Returns
staticThrows
InvalidArgumentExceptionFor invalid header names or values.
withMethod() ¶ public
withMethod(string $method): static
Return an instance with the provided HTTP method.
While HTTP method names are typically all uppercase characters, HTTP method names are case-sensitive and thus implementations SHOULD NOT modify the given string.
This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that has the changed request method.
Parameters
-
string$method
Returns
staticwithProtocolVersion() ¶ public
withProtocolVersion(string $version): static
Return an instance with the specified HTTP protocol version.
Parameters
-
string$version HTTP protocol version.
Returns
staticThrows
InvalidArgumentExceptionFor unsupported protocol versions.
withRequestTarget() ¶ public
withRequestTarget(string $requestTarget): static
Return an instance with the specific request-target.
If the request needs a non-origin-form request-target — e.g., for specifying an absolute-form, authority-form, or asterisk-form — this method may be used to create an instance with the specified request-target, verbatim.
This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that has the changed request target.
Parameters
-
string$requestTarget
Returns
staticwithUri() ¶ public
withUri(UriInterface $uri, bool $preserveHost = false): static
Returns an instance with the provided URI.
This method MUST update the Host header of the returned request by default if the URI contains a host component. If the URI does not contain a host component, any pre-existing Host header MUST be carried over to the returned request.
You can opt-in to preserving the original state of the Host header by
setting $preserveHost to true. When $preserveHost is set to
true, this method interacts with the Host header in the following ways:
- If the Host header is missing or empty, and the new URI contains a host component, this method MUST update the Host header in the returned request.
- If the Host header is missing or empty, and the new URI does not contain a host component, this method MUST NOT update the Host header in the returned request.
- If a Host header is present and non-empty, this method MUST NOT update the Host header in the returned request.
This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that has the new UriInterface instance.
Parameters
-
UriInterface$uri -
bool$preserveHost optional
Returns
staticwithoutHeader() ¶ public
withoutHeader(string $name): static
Return an instance without the specified header.
Parameters
-
string$name Case-insensitive header name.
Returns
staticProperty Detail
$headerNames ¶ protected
Map of lowercased header name to the original name used to register the header.
Type
array<non-empty-string, non-empty-string>$headers ¶ protected
List of all registered headers, as original name => list of values.
Type
array<non-empty-string, non-empty-list<string>>