Http4s backend

This backend is based on http4s (blaze client) and is asynchronous. To use, add the following dependency to your project:

"com.softwaremill.sttp.client" %% "http4s-backend" % "2.1.2"

To create the backend, you’ll need to provide a cats.effect.Blocker instance, which should be passed as a parameter, when defining the backend:

import sttp.client.http4s._

val blocker: cats.effect.Blocker = ...

Http4sBackend.usingClient(client, blocker).use { implicit backend => ... }
// or
Http4sBackend.usingClientBuilder(blazeClientBuilder, blocker).use { implicit backend => ... }
// or
Http4sBackend.usingDefaultClientBuilder(blocker).use { implicit backend => ... }

The backend can be created for any type implementing the cats.effect.ConcurrentEffect typeclass, such as cats.effect.IO. Moreover, an implicit ContextShift will have to be in scope as well.

If a blocker instance is not available, a new one can be created, and the resource definition can be chained, e.g. as follows:

implicit val cs: ContextShift[IO] = IO.contextShift(scala.concurrent.ExecutionContext.global) // or another instance
Blocker[IO].flatMap(Http4sBackend.usingDefaultClientBuilder[IO](_)).use { implicit backend => ... }

Sending a request is a non-blocking, lazily-evaluated operation and results in a wrapped response. There’s a transitive dependency on http4s.

There are also other cats-effect-based backends, which don’t depend on http4s.

Please note that:

  • the backend does not support SttpBackendOptions,that is specifying proxy settings (proxies are not implemented in http4s, see this issue), as well as configuring the connect timeout
  • the backend does not support the RequestT.options.readTimeout option

Instead, all custom timeout configuration should be done by creating a org.http4s.client.Client[F], using org.http4s.client.blaze.BlazeClientBuilder[F] and passing it to the appropriate method of the Http4sBackend object.

The backend supports streaming using fs2. For usage details, see the documentation on streaming using fs2.

The backend doesn’t support websockets.