Skip to content

Instantly share code, notes, and snippets.

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

  • Save dacr/3bf189b3bf530b1d3b45848a2f804938 to your computer and use it in GitHub Desktop.

Select an option

Save dacr/3bf189b3bf530b1d3b45848a2f804938 to your computer and use it in GitHub Desktop.
http client using requests API / published by https://github.com/dacr/code-examples-manager #d2da7227-1f3c-4b8e-b498-18a5c7815ad5/d8f610a55e5c7d1dff1016f2d29aa7c588d4f624
// summary : http client using requests API
// keywords : scala, requests, @testable
// publish : gist
// authors : David Crosson
// license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt)
// id : d2da7227-1f3c-4b8e-b498-18a5c7815ad5
// created-on : 2020-10-10T18:21:18+02:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.4.2"
//> using dep "com.lihaoyi::requests:0.8.2"
//> using dep "com.lihaoyi::upickle:3.3.1"
//> using dep "org.json4s::json4s-jackson-core:4.0.7"
//> using dep "org.json4s::json4s-core:4.0.7"
//> using objectWrapper
// ---------------------
import upickle.default.ReadWriter
case class Person(first_name:String, last_name:String) derives ReadWriter
case class Response(json:Person) derives ReadWriter
println("------------------------------------------------ 1")
{
val response = requests.get("https://httpbin.org/get")
response.lines().foreach(println)
}
println("------------------------------------------------ 2")
{
val response = requests.post("https://httpbin.org/post", data = Map("name" -> "joe"))
response.lines().foreach(println)
}
println("------------------------------------------------ 3")
{
val response = requests.post("https://httpbin.org/post", data = """{"name":"joe"}""")
response.lines().foreach(println)
}
println("------------------------------------------------ 4")
{
val response = requests.post("https://httpbin.org/post", data = ujson.Obj("first_name"->"joe", "last_name"->"doe"))
println(upickle.default.read[Response](response.text()))
}
println("------------------------------------------------ 5")
{
import org.json4s._
import org.json4s.JsonDSL._
import org.json4s.jackson.JsonMethods.parse
implicit val formats = org.json4s.DefaultFormats
val response = requests.post("https://httpbin.org/post", data = ujson.Obj("first_name"->"joe", "last_name"->"doe"))
println( (parse(response.text()) \ "json").extractOpt[Person] )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment