CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (Github)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 6.0 API

  • Project:
    • CakePHP
      • CakePHP
      • Authentication
      • Authorization
      • Chronos
      • Elastic Search
      • Queue
  • Version:
    • 6.0
      • 6.0
      • 5.4
      • 5.3
      • 5.2
      • 5.1
      • 5.0
      • 4.6
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Global
  • Cake
    • AttributeResolver
    • Cache
    • Collection
    • Command
    • Console
    • Container
    • Controller
    • Core
    • Database
    • Datasource
    • Error
    • Event
    • Form
    • Http
      • Client
        • Adapter
        • Auth
        • Exception
      • Cookie
      • Exception
      • Link
      • Middleware
      • RateLimit
      • Response
      • Session
      • TestSuite
    • I18n
    • Lock
    • Log
    • Mailer
    • Network
    • ORM
    • Routing
    • TestSuite
    • Utility
    • Validation
    • View

Class Request

Implements methods for HTTP requests.

Used by Cake\Http\Client to contain request information for making requests.

Namespace: Cake\Http\Client

Constants

  • string
    METHOD_DELETE ¶
    'DELETE'

    HTTP DELETE method

  • string
    METHOD_GET ¶
    'GET'

    HTTP GET method

  • string
    METHOD_HEAD ¶
    'HEAD'

    HTTP HEAD method

  • string
    METHOD_OPTIONS ¶
    'OPTIONS'

    HTTP OPTIONS method

  • string
    METHOD_PATCH ¶
    'PATCH'

    HTTP PATCH method

  • string
    METHOD_POST ¶
    'POST'

    HTTP POST method

  • string
    METHOD_PUT ¶
    'PUT'

    HTTP PUT method

  • string
    METHOD_TRACE ¶
    'TRACE'

    HTTP TRACE method

  • int
    STATUS_ACCEPTED ¶
    202

    HTTP 202 code

  • int
    STATUS_CREATED ¶
    201

    HTTP 201 code

  • int
    STATUS_FOUND ¶
    302

    HTTP 302 code

  • int
    STATUS_MOVED_PERMANENTLY ¶
    301

    HTTP 301 code

  • int
    STATUS_NON_AUTHORITATIVE_INFORMATION ¶
    203

    HTTP 203 code

  • int
    STATUS_NO_CONTENT ¶
    204

    HTTP 204 code

  • int
    STATUS_OK ¶
    200

    HTTP 200 code

  • int
    STATUS_PERMANENT_REDIRECT ¶
    308

    HTTP 308 code

  • int
    STATUS_SEE_OTHER ¶
    303

    HTTP 303 code

  • int
    STATUS_TEMPORARY_REDIRECT ¶
    307

    HTTP 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
    string

    The HTTP method.

  • $protocol protected
    string

    The HTTP protocol version.

  • $requestTarget protected
    string|null

    The request target, if it has been explicitly set.

  • $stream protected
    Psr\Http\Message\StreamInterface

    The message body.

  • $uri protected
    Psr\Http\Message\UriInterface

    The 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 $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.

  • 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
void

assertHeaderName() ¶ protected

assertHeaderName(string $name): void

Validate a header name.

Parameters
string $name

The header name.

Returns
void
Throws
InvalidArgumentException
When 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
void

filterHeaderValue() ¶ 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
InvalidArgumentException
When a value is invalid.

getBody() ¶ public

getBody(): Psr\Http\Message\StreamInterface

Gets the body of the message.

Returns
Psr\Http\Message\StreamInterface

getHeader() ¶ 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
string

The 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
string

getMethod() ¶ public

getMethod(): string

Retrieves the HTTP method of the request.

Returns
string

getProtocolVersion() ¶ public

getProtocolVersion(): string

Retrieves the HTTP protocol version as a string.

Returns
string

HTTP 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
string

getUri() ¶ public

getUri(): UriInterface

Retrieves the URI instance.

This method MUST return a UriInterface instance.

Returns
UriInterface

hasHeader() ¶ public

hasHeader(string $name): bool

Checks if a header exists by the given case-insensitive name.

Parameters
string $name

Case-insensitive header name.

Returns
bool

removeHeader() ¶ protected

removeHeader(string $name): void

Remove a header from the current instance.

Parameters
string $name

Case-insensitive header name.

Returns
void

replaceHeader() ¶ 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
void

setContent() ¶ 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
$this

setMethod() ¶ protected

setMethod(string $method): void

Set and validate the HTTP method.

Parameters
string $method

The HTTP method.

Returns
void
Throws
InvalidArgumentException
When 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
static
Throws
InvalidArgumentException
For 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
static

withHeader() ¶ 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
static
Throws
InvalidArgumentException
For 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
static

withProtocolVersion() ¶ public

withProtocolVersion(string $version): static

Return an instance with the specified HTTP protocol version.

Parameters
string $version

HTTP protocol version.

Returns
static
Throws
InvalidArgumentException
For 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
static

withUri() ¶ 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
static

withoutHeader() ¶ public

withoutHeader(string $name): static

Return an instance without the specified header.

Parameters
string $name

Case-insensitive header name.

Returns
static

Property 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>>

$method ¶ protected

The HTTP method.

Type
string

$protocol ¶ protected

The HTTP protocol version.

Type
string

$requestTarget ¶ protected

The request target, if it has been explicitly set.

Type
string|null

$stream ¶ protected

The message body.

Type
Psr\Http\Message\StreamInterface

$uri ¶ protected

The request URI.

Type
Psr\Http\Message\UriInterface
OpenHub
Pingping
Linode
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (Github)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs