friendship ended with social-app. php is my new best friend
1--- 2title: Request API Reference 3description: API reference for the Request class in the Fetch HTTP client package 4--- 5 6# Request API Reference 7 8The complete API reference for the `Request` class in the Fetch HTTP client package. 9 10## Class Declaration 11 12```php 13namespace Fetch\Http; 14 15class Request extends BaseRequest implements RequestInterface 16{ 17 use RequestImmutabilityTrait; 18 19 // ... 20} 21``` 22 23## Constructor 24 25```php 26/** 27 * Create a new Request instance. 28 */ 29public function __construct( 30 string|Method $method, 31 string|UriInterface $uri, 32 array $headers = [], 33 $body = null, 34 string $version = '1.1', 35 ?string $requestTarget = null 36) 37``` 38 39## Static Factory Methods 40 41### HTTP Method Factories 42 43### `get()` 44 45Create a new GET request. 46 47```php 48public static function get(string|UriInterface $uri, array $headers = []): static 49``` 50 51### `post()` 52 53Create a new POST request. 54 55```php 56public static function post( 57 string|UriInterface $uri, 58 $body = null, 59 array $headers = [], 60 ContentType|string|null $contentType = null 61): static 62``` 63 64### `put()` 65 66Create a new PUT request. 67 68```php 69public static function put( 70 string|UriInterface $uri, 71 $body = null, 72 array $headers = [], 73 ContentType|string|null $contentType = null 74): static 75``` 76 77### `patch()` 78 79Create a new PATCH request. 80 81```php 82public static function patch( 83 string|UriInterface $uri, 84 $body = null, 85 array $headers = [], 86 ContentType|string|null $contentType = null 87): static 88``` 89 90### `delete()` 91 92Create a new DELETE request. 93 94```php 95public static function delete( 96 string|UriInterface $uri, 97 $body = null, 98 array $headers = [], 99 ContentType|string|null $contentType = null 100): static 101``` 102 103### `head()` 104 105Create a new HEAD request. 106 107```php 108public static function head(string|UriInterface $uri, array $headers = []): static 109``` 110 111### `options()` 112 113Create a new OPTIONS request. 114 115```php 116public static function options(string|UriInterface $uri, array $headers = []): static 117``` 118 119### Content Type Factories 120 121### `json()` 122 123Create a new Request instance with a JSON body. 124 125```php 126public static function json( 127 string|Method $method, 128 string|UriInterface $uri, 129 array $data, 130 array $headers = [] 131): static 132``` 133 134### `form()` 135 136Create a new Request instance with form parameters. 137 138```php 139public static function form( 140 string|Method $method, 141 string|UriInterface $uri, 142 array $formParams, 143 array $headers = [] 144): static 145``` 146 147### `multipart()` 148 149Create a new Request instance with multipart form data. 150 151```php 152public static function multipart( 153 string|Method $method, 154 string|UriInterface $uri, 155 array $multipart, 156 array $headers = [] 157): static 158``` 159 160## Request Target Methods 161 162### `getRequestTarget()` 163 164Get the request target (path for origin-form, absolute URI for absolute-form, authority for authority-form, or asterisk for asterisk-form). 165 166```php 167public function getRequestTarget(): string 168``` 169 170### `withRequestTarget()` 171 172Return an instance with the specific request target. 173 174```php 175public function withRequestTarget($requestTarget): static 176``` 177 178## Request Method Information 179 180### `getMethodEnum()` 181 182Get the method as an enum. 183 184```php 185public function getMethodEnum(): ?Method 186``` 187 188### `supportsRequestBody()` 189 190Check if the request method supports a request body. 191 192```php 193public function supportsRequestBody(): bool 194``` 195 196## Content Type Methods 197 198### `getContentTypeEnum()` 199 200Get the content type from the headers as an enum. 201 202```php 203public function getContentTypeEnum(): ?ContentType 204``` 205 206### `hasJsonContent()` 207 208Check if the request has JSON content. 209 210```php 211public function hasJsonContent(): bool 212``` 213 214### `hasFormContent()` 215 216Check if the request has form content. 217 218```php 219public function hasFormContent(): bool 220``` 221 222### `hasMultipartContent()` 223 224Check if the request has multipart content. 225 226```php 227public function hasMultipartContent(): bool 228``` 229 230### `hasTextContent()` 231 232Check if the request has text content. 233 234```php 235public function hasTextContent(): bool 236``` 237 238## Body Methods 239 240### `getBodyAsString()` 241 242Get the request body as a string. 243 244```php 245public function getBodyAsString(): string 246``` 247 248### `getBodyAsJson()` 249 250Get the request body as JSON. 251 252```php 253public function getBodyAsJson(bool $assoc = true, int $depth = 512, int $options = 0): mixed 254``` 255 256### `getBodyAsFormParams()` 257 258Get the request body as form parameters. 259 260```php 261public function getBodyAsFormParams(): array 262``` 263 264## Request Modification Methods 265 266### `withBody()` 267 268Return an instance with the specified body. 269 270```php 271public function withBody($body): static 272``` 273 274### `withContentType()` 275 276Set the content type of the request. 277 278```php 279public function withContentType(ContentType|string $contentType): static 280``` 281 282### `withQueryParam()` 283 284Set a query parameter on the request URI. 285 286```php 287public function withQueryParam(string $name, string|int|float|bool|null $value): static 288``` 289 290### `withQueryParams()` 291 292Set multiple query parameters on the request URI. 293 294```php 295public function withQueryParams(array $params): static 296``` 297 298### `withBearerToken()` 299 300Set an authorization header with a bearer token. 301 302```php 303public function withBearerToken(string $token): static 304``` 305 306### `withBasicAuth()` 307 308Set a basic authentication header. 309 310```php 311public function withBasicAuth(string $username, string $password): static 312``` 313 314### `withJsonBody()` 315 316Set a JSON body on the request. 317 318```php 319public function withJsonBody(array $data, int $options = 0): static 320``` 321 322### `withFormBody()` 323 324Set a form body on the request. 325 326```php 327public function withFormBody(array $data): static 328``` 329 330## PSR-7 Methods (from RequestImmutabilityTrait) 331 332These methods override the PSR-7 request methods to ensure immutability and proper type preservation. 333 334### `withAddedHeader()` 335 336Return an instance with the specified header appended with the given value. 337 338```php 339public function withAddedHeader($name, $value): static 340``` 341 342### `withoutHeader()` 343 344Return an instance without the specified header. 345 346```php 347public function withoutHeader($name): static 348``` 349 350### `withHeader()` 351 352Return an instance with the provided value replacing the specified header. 353 354```php 355public function withHeader($name, $value): static 356``` 357 358### `withProtocolVersion()` 359 360Return an instance with the specified protocol version. 361 362```php 363public function withProtocolVersion($version): static 364``` 365 366### `withUri()` 367 368Return an instance with the specified URI. 369 370```php 371public function withUri(UriInterface $uri, $preserveHost = false): static 372``` 373 374### `withMethod()` 375 376Return an instance with the provided HTTP method. 377 378```php 379public function withMethod($method): static 380```