2015-07-16 17:37:34 +02:00
|
|
|
.. _request-level-api:
|
2015-05-11 23:05:18 +02:00
|
|
|
|
|
|
|
|
Request-Level Client-Side API
|
|
|
|
|
=============================
|
|
|
|
|
|
2015-07-16 17:37:34 +02:00
|
|
|
The request-level API is the most convenient way of using Akka HTTP's client-side functionality. It internally builds upon the
|
|
|
|
|
:ref:`host-level-api` to provide you with a simple and easy-to-use way of retrieving HTTP responses from remote servers.
|
2015-05-11 23:05:18 +02:00
|
|
|
Depending on your preference you can pick the flow-based or the future-based variant.
|
|
|
|
|
|
2015-07-21 14:25:07 +02:00
|
|
|
.. note::
|
|
|
|
|
The request-level API is implemented on top of a connection pool that is shared inside the ActorSystem. A consequence of
|
|
|
|
|
using a pool is that long-running requests block a connection while running and starve other requests. Make sure not to use
|
|
|
|
|
the request-level API for long-running requests like long-polling GET requests. Use the :ref:`connection-level-api` instead.
|
2015-05-11 23:05:18 +02:00
|
|
|
|
|
|
|
|
Flow-Based Variant
|
|
|
|
|
------------------
|
|
|
|
|
|
2015-05-28 09:33:00 +02:00
|
|
|
The flow-based variant of the request-level client-side API is presented by the ``Http().superPool(...)`` method.
|
2015-05-11 23:05:18 +02:00
|
|
|
It creates a new "super connection pool flow", which routes incoming requests to a (cached) host connection pool
|
2015-07-16 17:37:34 +02:00
|
|
|
depending on their respective effective URIs.
|
2015-05-11 23:05:18 +02:00
|
|
|
|
2015-07-16 17:37:34 +02:00
|
|
|
The ``Flow`` returned by ``Http().superPool(...)`` is very similar to the one from the :ref:`host-level-api`, so the
|
2015-05-11 23:05:18 +02:00
|
|
|
:ref:`using-a-host-connection-pool` section also applies here.
|
|
|
|
|
|
|
|
|
|
However, there is one notable difference between a "host connection pool client flow" for the host-level API and a
|
|
|
|
|
"super-pool flow":
|
|
|
|
|
Since in the former case the flow has an implicit target host context the requests it takes don't need to have absolute
|
|
|
|
|
URIs or a valid ``Host`` header. The host connection pool will automatically add a ``Host`` header if required.
|
|
|
|
|
|
|
|
|
|
For a super-pool flow this is not the case. All requests to a super-pool must either have an absolute URI or a valid
|
|
|
|
|
``Host`` header, because otherwise it'd be impossible to find out which target endpoint to direct the request to.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Future-Based Variant
|
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
|
|
Sometimes your HTTP client needs are very basic. You simply need the HTTP response for a certain request and don't
|
|
|
|
|
want to bother with setting up a full-blown streaming infrastructure.
|
|
|
|
|
|
2015-05-28 09:33:00 +02:00
|
|
|
For these cases Akka HTTP offers the ``Http().singleRequest(...)`` method, which simply turns an ``HttpRequest`` instance
|
2015-05-11 23:05:18 +02:00
|
|
|
into ``Future[HttpResponse]``. Internally the request is dispatched across the (cached) host connection pool for the
|
|
|
|
|
request's effective URI.
|
|
|
|
|
|
|
|
|
|
Just like in the case of the super-pool flow described above the request must have either an absolute URI or a valid
|
|
|
|
|
``Host`` header, otherwise the returned future will be completed with an error.
|
|
|
|
|
|
2015-09-30 12:07:03 +02:00
|
|
|
Using the Future-Based API in Actors
|
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
When using the ``Future`` based API from inside an ``Actor``, all the usual caveats apply to how one should deal
|
|
|
|
|
with the futures completion. For example you should not access the Actors state from within the Future's callbacks
|
|
|
|
|
(such as ``map``, ``onComplete``, ...) and instead you should use the ``pipeTo`` pattern to pipe the result back
|
|
|
|
|
to the Actor as a message.
|
|
|
|
|
|
|
|
|
|
.. includecode:: ../../code/docs/http/scaladsl/HttpClientExampleSpec.scala
|
|
|
|
|
:include: single-request-in-actor-example
|
|
|
|
|
|
|
|
|
|
An ``ActorMaterializer`` instance needed for Http to perfom its duties can be obtained using the ``ImplicitMaterializer``
|
|
|
|
|
helper trait.
|
2015-05-11 23:05:18 +02:00
|
|
|
|
|
|
|
|
Example
|
|
|
|
|
-------
|
|
|
|
|
|
|
|
|
|
.. includecode:: ../../code/docs/http/scaladsl/HttpClientExampleSpec.scala
|
|
|
|
|
:include: single-request-example
|