Last active
June 15, 2024 07:58
-
-
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/a80b9accc4f1b4e1c764ed66f6e2fde941ea833f
This file contains hidden or 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.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