Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active February 3, 2026 20:24
Show Gist options
  • Select an option

  • Save dacr/7a4dc5c4eafce54264e4f578df4334bb to your computer and use it in GitHub Desktop.

Select an option

Save dacr/7a4dc5c4eafce54264e4f578df4334bb to your computer and use it in GitHub Desktop.
Simplest sttp http client library json example. / published by https://github.com/dacr/code-examples-manager #24dc9898-25f9-42af-b4e0-8e8b2cfe18dc/f4727ee7eb06e507cdd22d2d5e057cdc7d1e8b7a
// summary : Simplest sttp http client library json example.
// keywords : scala, sttp, http-client, json4s, json
// publish : gist
// authors : David Crosson
// license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt)
// id : 24dc9898-25f9-42af-b4e0-8e8b2cfe18dc
// created-on : 2019-05-19T16:59:54Z
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.4.2"
//> using dep "com.softwaremill.sttp.client3::core:3.7.0"
//> using dep "com.softwaremill.sttp.client3::json4s:3.7.0"
// ---------------------
import sttp.client3.*
import sttp.client3.json4s.*
import org.json4s.JValue
import org.json4s.jackson.Serialization
import org.json4s.*
implicit val serialization:Serialization.type = org.json4s.jackson.Serialization
implicit val formats:Formats = org.json4s.DefaultFormats
val result =
quickRequest
.get(uri"http://httpbin.org/ip")
.response(asJson[JValue])
.send(quick.backend)
// Various way to deal with the result
result.body.toSeq.flatMap{ case json => (json \ "origin").extractOpt[String] }.foreach(println)
result.body.map(json => (json \ "origin").extract[String] ).foreach(println)
result.body.toOption.map(_ \ "origin").flatMap(_.extractOpt[String]).foreach(println)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment