|
| 1 | +# HTTP Requests and Responses |
| 2 | + |
| 3 | +<show-structure for="chapter" depth="2"/> |
| 4 | + |
| 5 | +The component provides a set of HTTP structures that provide information |
| 6 | +about requests, responses, and their dependencies. |
| 7 | + |
| 8 | +<note> |
| 9 | +This component already included in the <code>boson-php/runtime</code>, |
| 10 | +so no separate installation is required when using the runtime. |
| 11 | +</note> |
| 12 | + |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +<tldr> |
| 17 | + <p> |
| 18 | + Via <a href="https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies">Composer</a>: |
| 19 | + </p> |
| 20 | + <p> |
| 21 | + <code lang="bash">composer require boson-php/http</code> |
| 22 | + </p> |
| 23 | +</tldr> |
| 24 | + |
| 25 | +**Requirements:** |
| 26 | + |
| 27 | +* `PHP ^8.4` |
| 28 | + |
| 29 | +## Usage |
| 30 | + |
| 31 | +The component provides request and response implementations. |
| 32 | + |
| 33 | +### Request |
| 34 | + |
| 35 | +The `Request` class represents an immutable HTTP request: |
| 36 | + |
| 37 | +```php |
| 38 | +use Boson\Component\Http\Request; |
| 39 | + |
| 40 | +// Create a new request with default values |
| 41 | +$request = new Request(); |
| 42 | + |
| 43 | +// Create a custom request |
| 44 | +$request = new Request( |
| 45 | + method: 'POST', |
| 46 | + url: 'https://example.com/api/users', |
| 47 | + headers: [ |
| 48 | + 'content-type' => 'application/json', |
| 49 | + 'authorization' => 'Bearer token123' |
| 50 | + ], |
| 51 | + body: '{"name": "John", "age": 30}' |
| 52 | +); |
| 53 | +``` |
| 54 | + |
| 55 | +All properties are immutable and can only be accessed, |
| 56 | +not modified. |
| 57 | + |
| 58 | +<tip> |
| 59 | +All request objects are created by the Boson itself within events, such as |
| 60 | +<a href="schemes-api-events.md">SchemeRequestReceived</a>. Therefore, to |
| 61 | +ensure that the object within an event or intention will <b>NOT be changed</b> |
| 62 | +and all listeners receive identical information, the request |
| 63 | +object <b>is immutable</b>. |
| 64 | +</tip> |
| 65 | + |
| 66 | +### Response |
| 67 | + |
| 68 | +The `Response` class represents a mutable HTTP response: |
| 69 | + |
| 70 | +```php |
| 71 | +use Boson\Component\Http\Response; |
| 72 | + |
| 73 | +// Create a new response with default values |
| 74 | +$response = new Response(); |
| 75 | + |
| 76 | +// Create a custom response |
| 77 | +$response = new Response( |
| 78 | + body: '<h1>Hello World</h1>', |
| 79 | + headers: [ |
| 80 | + 'content-type' => 'text/html', |
| 81 | + 'x-custom-header' => 'value' |
| 82 | + ], |
| 83 | + status: 200 |
| 84 | +); |
| 85 | + |
| 86 | +// Modify response |
| 87 | +$response->headers->add('x-new-header', 'new value'); |
| 88 | +$response->body = 'New content'; |
| 89 | +$response->status = 201; |
| 90 | +``` |
| 91 | + |
| 92 | +<tip> |
| 93 | +All responses are created by developer in any form. Therefore, for convenience, |
| 94 | +they are made <b>mutable</b> by default. |
| 95 | +</tip> |
| 96 | + |
| 97 | +### JSON Response |
| 98 | + |
| 99 | +The `JsonResponse` class extends `Response` to provide JSON-specific |
| 100 | +functionality: |
| 101 | + |
| 102 | +```php |
| 103 | +use Boson\Component\Http\JsonResponse; |
| 104 | + |
| 105 | +// Create a JSON response with default values |
| 106 | +$response = new JsonResponse(); |
| 107 | + |
| 108 | +// Create a custom JSON response |
| 109 | +$response = new JsonResponse( |
| 110 | + data: ['name' => 'John', 'age' => 30], |
| 111 | + headers: ['x-custom-header' => 'value'], |
| 112 | + status: 200 |
| 113 | +); |
| 114 | + |
| 115 | +// Create a JSON response with custom encoding flags |
| 116 | +$response = new JsonResponse( |
| 117 | + data: ['name' => 'John', 'age' => 30], |
| 118 | + jsonEncodingFlags: JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE |
| 119 | +); |
| 120 | +``` |
| 121 | + |
| 122 | + |
| 123 | +### Headers |
| 124 | + |
| 125 | +Both request and response use the `HeadersMap` class for header management. |
| 126 | +To get a list of headers, use the `headers` property in HTTP objects |
| 127 | +(for example, `$request->headers` or `$response->headers`). |
| 128 | + |
| 129 | +The request contains an immutable `HeadersMap` object which provides methods |
| 130 | +for reading header information. |
| 131 | + |
| 132 | +```php |
| 133 | +use Boson\Component\Http\HeadersMap; |
| 134 | + |
| 135 | +// Check if header exists |
| 136 | +if ($request->headers->has('content-type')) { |
| 137 | + // Get header value |
| 138 | + $contentType = $request->headers->first('content-type'); |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +While the response contains a mutable implementation of the list |
| 143 | +of `MutableHeadersMap` headers which represents not only methods of |
| 144 | +obtaining information, but also its modifications. |
| 145 | + |
| 146 | +```php |
| 147 | +use Boson\Component\Http\HeadersMap; |
| 148 | + |
| 149 | +// Add new header |
| 150 | +if (!$response->headers->has('x-custom-header')) { |
| 151 | + $response->headers->add('x-custom-header', 'value'); |
| 152 | +} |
| 153 | + |
| 154 | +// Remove header |
| 155 | +$response->headers->remove('content-type'); |
| 156 | +``` |
| 157 | + |
| 158 | +<note>The headers map is <b>case-insensitive</b> (lowercased) for header names</note> |
0 commit comments