HTTP Client-Server Example
Client and Server Example
zio-http-example/src/main/scala/example/ClientServer.scala
package example
import zio.ZIOAppDefault
import zio.http._
object ClientServer extends ZIOAppDefault {
val url = URL.decode("http://localhost:8080/hello").toOption.get
val app = Routes(
Method.GET / "hello" -> handler(Response.text("hello")),
Method.GET / "" -> handler(ZClient.batched(Request.get(url))),
).sandbox
val run =
Server.serve(app).provide(Server.default, Client.default).exitCode
}
Simple Client Example
zio-http-example/src/main/scala/example/SimpleClient.scala
package example
import zio._
import zio.http._
object SimpleClient extends ZIOAppDefault {
val url = URL.decode("https://jsonplaceholder.typicode.com/todos").toOption.get
val program = for {
client <- ZIO.service[Client]
res <- client.url(url).batched(Request.get("/"))
data <- res.body.asString
_ <- Console.printLine(data)
} yield ()
override val run = program.provide(Client.default)
}