The Scala Toolkit

How to send a request?

Language

You can require the entire toolkit in a single line:

//> using toolkit latest

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

//> using dep com.softwaremill.sttp.client4::core:4.0.0-M6

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

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

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

libraryDependencies += "com.softwaremill.sttp.client4" %% "core" % "4.0.0-M6"

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

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 sttp:

ivy"com.softwaremill.sttp.client4::core:4.0.0-M6"

Sending an HTTP request

The simplest way to send a request with sttp is quickRequest.

You can define a GET request with .get and send it with .send.

import sttp.client4.quick._
import sttp.client4.Response

val response: Response[String] = quickRequest
  .get(uri"https://httpbin.org/get")
  .send()

println(response.code)
// prints: 200

println(response.body)
// prints some JSON string
import sttp.client4.quick.*
import sttp.client4.Response

val response: Response[String] = quickRequest
  .get(uri"https://httpbin.org/get")
  .send()

println(response.code)
// prints: 200

println(response.body)
// prints some JSON string

A Response[String] contains a status code and a string body.

The request definition

The HTTP method and URI

To specify the HTTP method and URI of a quickRequest, you can use get, post, put, or delete.

To construct a URI you can use the uri interpolator, for e.g. uri"https://example.com". To learn more about that, see How to construct URIs and query parameters?.

The headers

By default, the quickRequest contains the “Accept-Encoding” and the “deflate” headers. To add more headers, you can call one of the header or headers overloads:

import sttp.client4.quick._

val request = quickRequest
  .get(uri"https://example.com")
  .header("Origin", "https://scala-lang.org")

println(request.headers)
// prints: Vector(Accept-Encoding: gzip, deflate, Origin: https://scala-lang.org)
import sttp.client4.quick.*

val request = quickRequest
  .get(uri"https://example.com")
  .header("Origin", "https://scala-lang.org")

println(request.headers)
// prints: Vector(Accept-Encoding: gzip, deflate, Origin: https://scala-lang.org)

sttp can also add “Content-Type” and “Content-Length” automatically if the request contains a body.

Authentication

If you need authentication to access a resource, you can use one of the auth.basic, auth.basicToken, auth.bearer or auth.digest methods.

import sttp.client4.quick._

// a request with a authentication
val request = quickRequest
  .get(uri"https://example.com")
  .auth.basic(user = "user", password = "***")
import sttp.client4.quick.*

// a request with a authentication
val request = quickRequest
  .get(uri"https://example.com")
  .auth.basic(user = "user", password = "***")

Contributors to this page: