Skip to content

Commit be0efb0

Browse files
committed
minor #17967 [HttpClient] Add UriTemplateHttpClient (alexandre-daubois)
This PR was merged into the 6.3 branch. Discussion ---------- [HttpClient] Add `UriTemplateHttpClient` Fixes #17965 Commits ------- 3ebc248 [HttpClient] Add `UriTemplateHttpClient`
2 parents 5a2af0f + 3ebc248 commit be0efb0

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

http_client.rst

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ Some options are described in this guide:
155155
* `Redirects`_
156156
* `Retry Failed Requests`_
157157
* `HTTP Proxies`_
158+
* `Using URI Templates`_
158159

159160
Check out the full :ref:`http_client config reference <reference-http-client>`
160161
to learn about all the options.
@@ -822,6 +823,83 @@ in your requests::
822823

823824
This setting won’t affect other clients.
824825

826+
Using URI Templates
827+
~~~~~~~~~~~~~~~~~~~
828+
829+
The :class:`Symfony\\Component\\HttpClient\\UriTemplateHttpClient` provides
830+
a client that eases the use of URI templates, as described in the `RFC 6570`_.
831+
Here is an example of how to use this client with URI templates::
832+
833+
$client = new UriTemplateHttpClient();
834+
835+
// This request will result on querying http://example.org/users?page=1
836+
$client->request('GET', 'http://example.org/{resource}{?page}', [
837+
'vars' => [
838+
'resource' => 'users',
839+
'page' => 1,
840+
],
841+
]);
842+
843+
When using this client in the framework context, all existing HTTP clients
844+
are decorated by the :class:`Symfony\\Component\\HttpClient\\UriTemplateHttpClient`.
845+
This means that URI template feature is enabled by default for all HTTP clients
846+
you may use in your application.
847+
848+
You can configure variables that will be replaced globally in all URI templates
849+
of your application:
850+
851+
.. configuration-block::
852+
853+
.. code-block:: yaml
854+
855+
# config/packages/framework.yaml
856+
framework:
857+
http_client:
858+
default_options:
859+
vars:
860+
- secret: 'secret-token'
861+
862+
.. code-block:: xml
863+
864+
<!-- config/packages/framework.xml -->
865+
<?xml version="1.0" encoding="UTF-8" ?>
866+
<container xmlns="http://symfony.com/schema/dic/services"
867+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
868+
xmlns:framework="http://symfony.com/schema/dic/symfony"
869+
xsi:schemaLocation="http://symfony.com/schema/dic/services
870+
https://symfony.com/schema/dic/services/services-1.0.xsd
871+
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
872+
873+
<framework:config>
874+
<framework:http-client>
875+
<framework:default-options>
876+
<framework:vars name="secret">secret-token</framework:vars>
877+
</framework:default-options>
878+
</framework:http-client>
879+
</framework:config>
880+
</container>
881+
882+
.. code-block:: php
883+
884+
// config/packages/framework.php
885+
use Symfony\Config\FrameworkConfig;
886+
887+
return static function (FrameworkConfig $framework) {
888+
$framework->httpClient()
889+
->defaultOptions()
890+
->vars(['secret' => 'secret-token'])
891+
;
892+
};
893+
894+
If you want to define your own logic to handle variables of URI templates, you
895+
can do so by redefining the ``http_client.uri_template_expander`` alias. Your
896+
service must be invokable.
897+
898+
.. versionadded:: 6.3
899+
900+
The :class:`Symfony\\Component\\HttpClient\\UriTemplateHttpClient` was
901+
introduced in Symfony 6.3.
902+
825903
Performance
826904
-----------
827905

@@ -2055,3 +2133,4 @@ you to do so, by yielding the exception from its body::
20552133
.. _`EventSource`: https://www.w3.org/TR/eventsource/#eventsource
20562134
.. _`idempotent method`: https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Idempotent_methods
20572135
.. _`SSRF`: https://portswigger.net/web-security/ssrf
2136+
.. _`RFC 6570`: https://www.rfc-editor.org/rfc/rfc6570

0 commit comments

Comments
 (0)