JSON handling

Add the JSON support module(s)

http4s-core does not include JSON support, but integration with three popular Scala JSON libraries are supported as modules.

Circe

The http4s team recommends circe. Only http4s-circe is required for basic interop with circe, but to follow this tutorial, install all three:

val http4sVersion = "0.18.26"

libraryDependencies ++= Seq(
  "org.http4s" %% "http4s-circe" % http4sVersion,
  // Optional for auto-derivation of JSON codecs
  "io.circe" %% "circe-generic" % "0.9.3",
  // Optional for string interpolation to JSON model
  "io.circe" %% "circe-literal" % "0.9.3"
)

addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full)

Argonaut

Circe is a fork of argonaut, another popular JSON library in the Scala community. The functionality is similar:

libraryDependencies += Seq(
  "org.http4s" %% "http4s-argonaut" % http4sVersion,
  // Optional for auto-derivation of JSON codecs
  "com.github.alexarchambault" %% "argonaut-shapeless_6.2" % "1.2.0-M6"
)

For those not ready to upgrade to argonaut-6.2, an http4s-argonaut61 is also available. It is source compatible, but compiled against Argonaut 6.1.

Json4s

Json4s is less functionally pure than Circe or Argonaut, but older and integrated with many Scala libraries. It comes with two backends. You should pick one of these dependencies:

libraryDependencies += "org.http4s" %% "http4s-json4s-native" % http4sVersion
libraryDependencies += "org.http4s" %% "http4s-json4s-jackson" % http4sVersion

There is no extra codec derivation library for json4s, as it generally bases its codecs on runtime reflection.

Sending raw JSON

Let’s create a function to produce a simple JSON greeting with circe. First, the imports:

import cats.effect._
import io.circe._
import io.circe.literal._
import org.http4s._
import org.http4s.dsl.io._

Then the actual code:

def hello(name: String): Json =
  json"""{"hello": $name}"""

val greeting = hello("world")
// greeting: Json = JObject(object[hello -> "world"])

We now have a JSON value, but we don’t have enough to render it:

Ok(greeting).unsafeRunSync
// error: Cannot convert from io.circe.Json to an Entity, because no EntityEncoder[cats.effect.IO, io.circe.Json] instance could be found.
// Ok(greeting).unsafeRunSync
// ^^^^^^^^^^^^

To encode a Scala value of type A into an entity, we need an EntityEncoder[A] in scope. The http4s-circe module includes a org.http4s.circe object, which gives us exactly this for an io.circe.Json value:

import org.http4s.circe._

Ok(greeting).unsafeRunSync
// res1: Response[IO] = Response(
//   Status(200),
//   HttpVersion(1, 1),
//   Headers(Content-Type(MediaType(application/json), None), Content-Length(17L)),
//   Stream(..),
//   org.http4s.AttributeMap@8cf85da
// )

The same EntityEncoder[Json] we use on server responses is also useful on client requests:

import org.http4s.client._
import org.http4s.client.dsl.io._

POST(uri("/hello"), json"""{"name": "Alice"}""").unsafeRunSync
// res2: Request[IO] = Request(
//   Method("POST"),
//   Uri(None, None, "/hello", Query(), None),
//   HttpVersion(1, 1),
//   Headers(Content-Type(MediaType(application/json), None), Content-Length(16L)),
//   Stream(..),
//   org.http4s.AttributeMap@8cf85da
// )

Encoding case classes as JSON

These JSON literals are nice, but in real apps, we prefer to operate on case classes and use JSON as a serialization format near the edge of the world.

Let’s define a couple case classes:

case class Hello(name: String)
case class User(name: String)

To transform a value of type A into Json, circe uses an io.circe.Encoder[A]. With circe’s syntax, we can convert any value to JSON as long as an implicit Encoder is in scope:

import io.circe.syntax._
Hello("Alice").asJson
// error: could not find implicit value for parameter encoder: io.circe.Encoder[repl.Session.App.Hello]
//   Encoder.instance { hello: Hello =>
//                             ^

Oops! We haven’t told Circe how we want to encode our case class. Let’s provide an encoder:

implicit val HelloEncoder: Encoder[Hello] =
  Encoder.instance { hello: Hello =>
    json"""{"hello": ${hello.name}}"""
  }
// HelloEncoder: Encoder[Hello] = io.circe.Encoder$$anon$18@13e16663

Hello("Alice").asJson
// res4: Json = JObject(object[hello -> "Alice"])

That was easy, but gets tedious for applications dealing in lots of types. Fortunately, circe can automatically derive an encoder for us, using the field names of the case class as key names in a JSON object:

import io.circe.generic.auto._

User("Alice").asJson
// res5: Json = JObject(object[name -> "Alice"])

Equipped with an Encoder and .asJson, we can send JSON in requests and responses for our case classes:

Ok(Hello("Alice").asJson).unsafeRunSync
// res6: Response[IO] = Response(
//   Status(200),
//   HttpVersion(1, 1),
//   Headers(Content-Type(MediaType(application/json), None), Content-Length(17L)),
//   Stream(..),
//   org.http4s.AttributeMap@8cf85da
// )
POST(uri("/hello"), User("Bob").asJson).unsafeRunSync
// res7: Request[IO] = Request(
//   Method("POST"),
//   Uri(None, None, "/hello", Query(), None),
//   HttpVersion(1, 1),
//   Headers(Content-Type(MediaType(application/json), None), Content-Length(14L)),
//   Stream(..),
//   org.http4s.AttributeMap@8cf85da
// )

If within some route we serve json only, we can use:

{
import org.http4s.circe.CirceEntityEncoder._
}

Thus there’s no more need in calling asJson on result. However, it may introduce ambiguity errors when we also build some json by hand within the same scope.

Receiving raw JSON

Just as we needed an EntityEncoder[JSON] to send JSON from a server or client, we need an EntityDecoder[JSON] to receive it.

The org.http4s.circe._ package provides an implicit EntityDecoder[Json]. This makes it very easy to decode a request or response body to JSON using the as syntax:

Ok("""{"name":"Alice"}""").flatMap(_.as[Json]).unsafeRunSync
// res9: Json = JObject(object[name -> "Alice"])
POST(uri("/hello"),"""{"name":"Bob"}""").flatMap(_.as[Json]).unsafeRunSync
// res10: Json = JObject(object[name -> "Bob"])

Like sending raw JSON, this is useful to a point, but we typically want to get to a typed model as quickly as we can.

Decoding JSON to a case class

To get from an HTTP entity to Json, we use an EntityDecoder[Json]. To get from Json to any type A, we need an io.circe.Decoder[A]. http4s-circe provides the jsonOf function to make the connection all the way from HTTP to your type A. Specifically, jsonOf[A] takes an implicit Decoder[A] and makes a EntityDecoder[A]:

implicit val userDecoder = jsonOf[IO, User]
// userDecoder: EntityDecoder[IO, User] = org.http4s.EntityDecoder$$anon$2@1288e2dc
Ok("""{"name":"Alice"}""").flatMap(_.as[User]).unsafeRunSync
// res11: User = User("Alice")
POST(uri("/hello"), """{"name":"Bob"}""").flatMap(_.as[User]).unsafeRunSync
// res12: User = User("Bob")

If we are always decoding from JSON to a typed model, we can use the following import:

{
import org.http4s.circe.CirceEntityDecoder._
}

This creates an EntityDecoder[A] for every A that has a Decoder instance.

However, be cautious when using this. Having this implicit in scope does mean that we would always try to decode HTTP entities from JSON (even if it is XML or plain text, for instance).

For more convenience there is import combining both encoding and decoding derivation:

{
import org.http4s.circe.CirceEntityCodec._
}

Putting it all together

A Hello world service

Our hello world service will parse a User from a request and offer a proper greeting.

import cats.effect._

import io.circe._
import io.circe.generic.auto._
import io.circe.syntax._

import org.http4s._
import org.http4s.circe._
import org.http4s.dsl.io._

case class User(name: String)
case class Hello(greeting: String)

implicit val decoder = jsonOf[IO, User]
// decoder: EntityDecoder[IO, User] = org.http4s.EntityDecoder$$anon$2@6137c09b

val jsonService = HttpService[IO] {
  case req @ POST -> Root / "hello" =>
    for {
	  // Decode a User request
	  user <- req.as[User]
	  // Encode a hello response
	  resp <- Ok(Hello(user.name).asJson)
    } yield (resp)
}
// jsonService: HttpService[IO] = Kleisli(
//   org.http4s.HttpService$$$Lambda$12903/708292025@7f8dbfb7
// )

import org.http4s.server.blaze._
val builder = BlazeBuilder[IO].bindHttp(8080).mountService(jsonService, "/").start
// builder: IO[server.Server[IO]] = Delay(
//   org.http4s.server.blaze.BlazeBuilder$$Lambda$13116/532336336@67c71c5b
// )
val blazeServer = builder.unsafeRunSync
// blazeServer: server.Server[IO] = BlazeServer(/127.0.0.1:8080)

A Hello world client

Now let’s make a client for the service above:

import org.http4s.client.blaze._
import org.http4s.Request
import cats.effect.IO
import io.circe.generic.auto._

// Decode the Hello response
def helloClient(name: String): IO[Hello] = {
  // Encode a User request
  val req = Request[IO](method = POST, uri = uri("http://localhost:8080/hello")).withBody(User(name).asJson)
  // Create a client
  Http1Client[IO]().flatMap { httpClient =>
    // Decode a Hello response
    httpClient.expect(req)(jsonOf[IO, Hello])
  }
}

Finally, we post User("Alice") to our Hello service and expect Hello("Alice") back:

val helloAlice = helloClient("Alice")
// helloAlice: IO[Hello] = Bind(
//   Delay(org.http4s.client.blaze.Http1Client$$$Lambda$13131/298336348@328c9c7d),
//   <function1>
// )
helloAlice.unsafeRunSync
// res16: Hello = Hello("Alice")