Skip to content

Commit 3cb560b

Browse files
[HttpClient] doc how to cancel a request
1 parent e0a86d1 commit 3cb560b

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

components/http_client.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,29 @@ response sequentially instead of waiting for the entire response::
303303
fwrite($fileHandler, $chunk->getContent());
304304
}
305305

306+
Canceling Responses
307+
~~~~~~~~~~~~~~~~~~~
308+
309+
To abort a request (e.g. because it didn't answer in due time, or you want to
310+
fetch only the first bytes of the response, etc.), you can either:
311+
312+
* use the ``cancel()`` method of ``ResponseInterface``::
313+
314+
$response->cancel()
315+
316+
* throw an exception from a progress callback::
317+
318+
$response = $client->request('GET', 'https://..;', [
319+
'on_progress' => function (int $dlNow, int $dlSize, array $info): void {
320+
// ...
321+
322+
throw new \MyException();
323+
},
324+
]);
325+
326+
The exception will be wrapped in a ``TransportExceptionInterface`` and will
327+
abort the request.
328+
306329
Handling Exceptions
307330
~~~~~~~~~~~~~~~~~~~
308331

0 commit comments

Comments
 (0)