VOOZH about

URL: https://deepwiki.com/hypervel/http-client/4.3-request

⇱ Request | hypervel/http-client | DeepWiki


Loading...
Menu

Request

The Request class wraps PSR-7's RequestInterface to provide simplified access to HTTP request data. It handles automatic body parsing, header inspection, content type detection, and query parameter management. The class implements ArrayAccess for read-only array-style data access and uses the Macroable trait for runtime extension.

Related Pages: See PendingRequest for request building and configuration, and Response for response handling.

Primary Source: src/Request.php1-279

Class Overview

The Request class provides a simplified interface over PSR-7's RequestInterface for reading request data.

Core Capabilities:

  • Access request metadata: method(), url(), body()
  • Header inspection: headers(), header(), hasHeader(), hasHeaders()
  • Content type detection: isForm(), isJson(), isMultipart()
  • Automatic body parsing: data() for form/JSON payloads
  • Query parameter access: query(), withQuery(), withoutQuery()
  • Multipart file validation: hasFile()
  • Array-style access via ArrayAccess (read-only)

Key Characteristics:

CharacteristicImplementation
Wrapped ClassPsr\Http\Message\RequestInterface
ImplementsArrayAccess
TraitHyperf\Macroable\Macroable
MutabilityImmutable (array access is read-only, query methods return new instances)
Data ParsingLazy-loaded and cached in $data property

Sources: src/Request.php14-28

Class Structure and Dependencies

Diagram: Request Class Architecture


Sources: src/Request.php1-279

Request Metadata Access

The Request class provides simple methods for retrieving basic request information:

HTTP Method


Returns the HTTP method (GET, POST, PUT, DELETE, etc.) from the underlying PSR-7 request.

Sources: src/Request.php33-36

Request URL


Returns the full URL of the request as a string by casting the PSR-7 URI object.

Sources: src/Request.php41-44

Body Content


Returns the raw body content of the request as a string.

Sources: src/Request.php103-106

Header Access and Validation

The Request class provides multiple methods for working with HTTP headers:

MethodPurposeReturn Type
hasHeader($key, $value = null)Check if header exists, optionally with specific value(s)bool
hasHeaders($headers)Check multiple headers at oncebool
header($key)Get all values for a specific headerarray
headers()Get all request headersarray

Header Checking Logic


Implementation Details:

Sources: src/Request.php49-98

Content Type Detection

The Request class provides three methods for detecting request body content types based on the Content-Type header:

Content Type Detection Methods

MethodDetectsImplementation
isForm()application/x-www-form-urlencodedExact match on Content-Type header
isJson()JSON content typesSubstring match for "json" in Content-Type
isMultipart()multipart/form-dataSubstring match for "multipart" in Content-Type

Sources: src/Request.php168-189

Diagram: Content Type Detection Logic


Sources: src/Request.php168-189

Data Extraction and Body Parsing

The Request class provides intelligent data extraction that automatically parses request bodies based on content type:

Diagram: data() Method Decision Flow


Methods and Behavior

MethodVisibilityPurposeCaching
data()publicReturns decoded form/JSON dataVia parameters() or json()
parameters()protectedParses form-encoded bodyCached in $data property
json()protectedParses JSON bodyCached in $data property
withData($data)publicManually set decoded dataSets $data property

Implementation Notes:

Sources: src/Request.php127-199

Query Parameter Management

The Request class provides methods for inspecting and manipulating URL query parameters:

Query Parameter Methods































MethodOperationReturnMutates Request
query()Parses and returns query parametersarrayNo
withQuery($query)Merges parameters into query stringNew RequestImmutable (returns new)
withoutQuery($keys)Removes specified parametersNew RequestImmutable (returns new)

Diagram: Query Parameter Methods


Sources: src/Request.php201-226

Multipart File Validation

For multipart requests, the Request class provides file validation:


Validation Logic:

  1. Checks if request is multipart via isMultipart()
  2. Searches $data array for matching file entries
  3. Optionally matches file contents and filename

Filter Criteria:

ParameterPurposeOptional
$nameFile input field nameNo
$valueExpected file contentsYes
$filenameExpected filenameYes

Sources: src/Request.php111-122

ArrayAccess Implementation

The Request class implements ArrayAccess to provide array-style access to request data. The implementation is read-only: write operations throw LogicException.

Diagram: ArrayAccess Methods


Behavior Summary

OperationMethodBehavior
Read checkisset($request['key'])Checks if key exists in data()
Read access$request['key']Returns value from data() array
Write$request['key'] = $valueThrows LogicException
Unsetunset($request['key'])Throws LogicException

Important: The ArrayAccess implementation is read-only. Attempts to modify data throw exceptions with the message: "Request data may not be mutated using array access."

Sources: src/Request.php237-278

PSR-7 Interoperability

The Request class maintains full compatibility with PSR-7 standards:


This method provides direct access to the underlying PSR-7 RequestInterface object, allowing integration with other PSR-7 compatible libraries and tools.

Use Cases:

  • Passing requests to PSR-7 middleware
  • Integration with PSR-15 request handlers
  • Compatibility with PSR-18 HTTP clients
  • Custom request transformations

Sources: src/Request.php231-234

Runtime Extension with Macros

The Request class uses the Macroable trait from hyperf/macroable, allowing custom methods to be added at runtime:


This enables developers to extend Request functionality without modifying the class itself. For more information about using macros, see Runtime Extension with Macros.

Sources: src/Request.php16

Complete Method Reference

Core Data Access

MethodReturn TypePurpose
method()stringGet HTTP method
url()stringGet full URL
body()stringGet raw body content
data()arrayGet parsed form/JSON data

Header Methods

MethodReturn TypePurpose
header($key)arrayGet specific header values
headers()arrayGet all headers
hasHeader($key, $value)boolCheck header existence/value
hasHeaders($headers)boolCheck multiple headers

Content Type Detection

MethodReturn TypePurpose
isForm()boolCheck if form-encoded
isJson()boolCheck if JSON
isMultipart()boolCheck if multipart

Query Parameters

MethodReturn TypePurpose
query()arrayGet query parameters
withQuery($query)RequestAdd/merge query parameters
withoutQuery($keys)RequestRemove query parameters

File Handling

MethodReturn TypePurpose
hasFile($name, $value, $filename)boolCheck for multipart file

Data Management

MethodReturn TypePurpose
withData($data)RequestSet decoded data manually

PSR-7 Compatibility

MethodReturn TypePurpose
toPsrRequest()RequestInterfaceGet underlying PSR-7 request

Sources: src/Request.php1-279