The Scala Toolkit

How to send and receive JSON?

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"

HTTP and JSON

JSON is a common format for HTTP request and response bodies.

In the examples below, we use the GitHub REST API. You will need a secret GitHub authentication token to run the programs. Do not share your token with anyone.

Sending and receiving JSON

To send a JSON request and parse a JSON response we use sttp in combination with uJson.

Sending JSON

To send JSON, you can construct a uJson.Value and write it as a string in the body of the request.

In the following example we use GitHub users endpoint to update the profile of the authenticated user. We provide the new location and bio of the profile in a JSON object.

import sttp.client4.quick._

val json = ujson.Obj(
  "location" -> "hometown",
  "bio" -> "Scala programmer"
)

val response = quickRequest
  .patch(uri"https://api.github.com/user")
  .auth.bearer(sys.env("GITHUB_TOKEN"))
  .header("Content-Type", "application/json")
  .body(ujson.write(json))
  .send()

println(response.code)
// prints: 200

println(response.body)
// prints the full updated profile in JSON
import sttp.client4.quick.*

val json = ujson.Obj(
  "location" -> "hometown",
  "bio" -> "Scala programmer"
)

val response = quickRequest
  .patch(uri"https://api.github.com/user")
  .auth.bearer(sys.env("GITHUB_TOKEN"))
  .header("Content-Type", "application/json")
  .body(ujson.write(json))
  .send()

println(response.code)
// prints: 200

println(response.body)
// prints the full updated profile in JSON

Before running the program, set the GITHUB_TOKEN environment variable. After running it, you should see your new bio and location on your GitHub profile.

Parsing JSON from the response

To parse JSON from the response of a request, you can use ujson.read.

Again we use the GitHub user endpoint, this time to get the authenticated user.

import sttp.client4.quick._

val response = quickRequest
  .get(uri"https://api.github.com/user")
  .auth.bearer(sys.env("GITHUB_TOKEN"))
  .send()

val json = ujson.read(response.body)

println(json("login").str)
// prints your login
import sttp.client4.quick.*

val response = quickRequest
  .get(uri"https://api.github.com/user")
  .auth.bearer(sys.env("GITHUB_TOKEN"))
  .send()

val json = ujson.read(response.body)

println(json("login").str)
// prints your login

Before running the program, set the GITHUB_TOKEN environment variable. Running the program should print your own login.

Sending and receiving Scala objects using JSON

Alternatively, you can use uPickle to send or receive Scala objects using JSON. Read the following to learn How to serialize an object to JSON and How to deserialize JSON to an object.

Contributors to this page: