The Scala Toolkit

How to send a request with a body?

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 a request with a string body

To send a POST request with a string body, you can chain post and body on a quickRequest:

import sttp.client4.quick._

val response = quickRequest
  .post(uri"https://example.com/")
  .body("Lorem ipsum")
  .send()

println(response.code)
// prints: 200
import sttp.client4.quick.*

val response = quickRequest
  .post(uri"https://example.com/")
  .body("Lorem ipsum")
  .send()

println(response.code)
// prints: 200

In a request with string body, sttp adds the Content-Type: text/plain; charset=utf-8 header and computes the Content-Length header.

Binary data

The body method can also take a Array[Byte], a ByteBuffer or an InputStream.

val bytes: Array[Byte] = "john".getBytes
val request = quickRequest.post(uri"https://example.com/").body(bytes)

The binary body of a request is sent with Content-Type: application/octet-stream.

Learn more in the sttp documentation chapter about request bodies.

Contributors to this page: