The Scala Toolkit

What else can sttp do?

Language

Using Scala CLI, you can require the entire toolkit in a single line:

//> using toolkit latest

Alternatively, you can require just a specific version of UPickle:

//> using dep com.lihaoyi::upickle:3.1.0

In your build.sbt file, you can add the dependency on the Toolkit:

lazy val example = project.in(file("example"))
  .settings(
    scalaVersion := "3.3.3",
    libraryDependencies += "org.scala-lang" %% "toolkit" % "0.1.7"
  )

Alternatively, you can require just a specific version of UPickle:

libraryDependencies += "com.lihaoyi" %% "upickle" % "3.1.0"

In your build.sc file, you can add the dependency to the upickle library:

object example extends ScalaModule {
  def scalaVersion = "3.3.3"
  def ivyDeps =
    Agg(
      ivy"org.scala-lang::toolkit:0.1.7"
    )
}

Alternatively, you can require just a specific version of UPickle:

ivy"com.lihaoyi::upickle:3.1.0"

Asynchronous requests

To send a request asynchronously you can use a DefaultFutureBackend:

import scala.concurrent.Future
import sttp.client4._

val asyncBackend = DefaultFutureBackend()
val response: Future[Response[String]] = quickRequest
  .get(uri"https://example.com")
  .send(asyncBackend)
import scala.concurrent.Future
import sttp.client4.*

val asyncBackend = DefaultFutureBackend()
val response: Future[Response[String]] = quickRequest
  .get(uri"https://example.com")
  .send(asyncBackend)

You can learn more about Future-based backends in the sttp documentation.

sttp supports other asynchronous wrappers such as Monix Tasks, cats-effect Effects, ZIO’s ZIO type, and more. You can see the full list of supported backends here.

Websockets

You can use a DefaultFutureBackend to open a websocket, as follows.

import scala.concurrent.duration.Duration
import scala.concurrent.{Await, Future}
import scala.concurrent.ExecutionContext.Implicits.global

import sttp.client4._
import sttp.ws.WebSocket

val asyncBackend = DefaultFutureBackend()

def useWebSocket(ws: WebSocket[Future]): Future[Unit] =
  for {
    _ <- ws.sendText("Hello")
    text <- ws.receiveText()
  } yield {
    println(text)
  }

val response = quickRequest
  .get(uri"wss://ws.postman-echo.com/raw")
  .response(asWebSocketAlways(useWebSocket))
  .send(asyncBackend)

Await.result(response, Duration.Inf)
// prints: Hello
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, Future}
import scala.concurrent.ExecutionContext.Implicits.global

import sttp.client4.*
import sttp.ws.WebSocket

val asyncBackend = DefaultFutureBackend()

def useWebSocket(ws: WebSocket[Future]): Future[Unit] =
  for
    _ <- ws.sendText("Hello")
    text <- ws.receiveText()
  yield
    println(text)

val response = quickRequest
  .get(uri"wss://ws.postman-echo.com/raw")
  .response(asWebSocketAlways(useWebSocket))
  .send(asyncBackend)

Await.result(response, Duration.Inf)
// prints: Hello

Learn more about WebSockets in sttp documentation.

More features

You can discover many more features such as streaming, logging, timeouts, and more in sttp documentation.

Contributors to this page: