Skip to content

Commit 697ddf3

Browse files
committed
feature #18247 [HttpClient] Add JsonMockResponse (alamirault)
This PR was squashed before being merged into the 6.3 branch. Discussion ---------- [HttpClient] Add `JsonMockResponse` Fix #18230 Commits ------- b68944c [HttpClient] Add `JsonMockResponse`
2 parents e5c4b3d + b68944c commit 697ddf3

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

http_client.rst

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ Retry Over Several Base URIs
740740

741741
The ``RetryableHttpClient`` can be configured to use multiple base URIs. This
742742
feature provides increased flexibility and reliability for making HTTP
743-
requests. Pass an array of base URIs as option ``base_uri`` when making a
743+
requests. Pass an array of base URIs as option ``base_uri`` when making a
744744
request::
745745

746746
$response = $client->request('GET', 'some-page', [
@@ -752,7 +752,7 @@ request::
752752
],
753753
]);
754754

755-
When the number of retries is higher than the number of base URIs, the
755+
When the number of retries is higher than the number of base URIs, the
756756
last base URI will be used for the remaining retries.
757757

758758
If you want to shuffle the order of base URIs for each retry attempt, nest the
@@ -770,13 +770,13 @@ base URIs you want to shuffle in an additional array::
770770
],
771771
]);
772772

773-
This feature allows for a more randomized approach to handling retries,
773+
This feature allows for a more randomized approach to handling retries,
774774
reducing the likelihood of repeatedly hitting the same failed base URI.
775775

776-
By using a nested array for the base URI, you can use this feature
776+
By using a nested array for the base URI, you can use this feature
777777
to distribute the load among many nodes in a cluster of servers.
778778

779-
You can also configure the array of base URIs using the ``withOptions()``
779+
You can also configure the array of base URIs using the ``withOptions()``
780780
method::
781781

782782
$client = $client->withOptions(['base_uri' => [
@@ -2032,6 +2032,30 @@ Then configure Symfony to use your callback:
20322032
;
20332033
};
20342034
2035+
To return json, you would normally do::
2036+
2037+
use Symfony\Component\HttpClient\Response\MockResponse;
2038+
2039+
$response = new MockResponse(json_encode([
2040+
'foo' => 'bar',
2041+
]), [
2042+
'response_headers' => [
2043+
'content-type' => 'application/json',
2044+
],
2045+
]);
2046+
2047+
You can use :class:`Symfony\\Component\\HttpClient\\Response\\JsonMockResponse` instead::
2048+
2049+
use Symfony\Component\HttpClient\Response\JsonMockResponse;
2050+
2051+
$response = new JsonMockResponse([
2052+
'foo' => 'bar',
2053+
]);
2054+
2055+
.. versionadded:: 6.3
2056+
2057+
The ``JsonMockResponse`` was introduced in Symfony 6.3.
2058+
20352059
Testing Request Data
20362060
~~~~~~~~~~~~~~~~~~~~
20372061

0 commit comments

Comments
 (0)