Last active
May 27, 2023 15:30
-
-
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/d3ba91ae0de2515dafd3c1b7afb6e331e20fcd67
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// summary : http client using requests API | |
// keywords : scala, requests, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// 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.3.0" | |
//> using dep "com.lihaoyi::requests:0.8.0" | |
//> using dep "com.lihaoyi::upickle:3.1.0" | |
//> using dep "org.json4s::json4s-jackson-core:4.0.6" | |
//> using dep "org.json4s::json4s-core:4.0.6" | |
////> 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