Search code examples
scalarestfinagleargonaut

Errors when sending data in body using Finagle/Finch in Scala


I am using Finagle/Finch and I get this error:

diverging implicit expansion for type argonaut.DecodeJson[A] starting 
  with method MapDecodeJson in trait DecodeJsons
diverging implicit expansion for type argonaut.DecodeJson[V] starting 
  with method MapDecodeJson in trait DecodeJsons
not enough arguments for method body: (implicit d: 
 io.finch.Decode.Aux[A,CT], implicit ct: 
 scala.reflect.ClassTag[A])io.finch.Endpoint[A]. Unspecified value 
 parameters d, ct.

For this code:

def sendPost(db: CommDb): Endpoint[String] =
  post("posts" :: body.as[String]) { s: String =>
    Ok("success")
  }

I have no idea how to resolve this.


Solution

  • The body API has changed in Finch 0.11. Just change your body call to body[CT, Foo] (where CT is content-type) and you should get that compiled. One thing: String body is sort of a special case, so you might want to use stringBody (no type parameters) instead since body is biased towards decoding a payload with a given JSON/XML/whatever decoder.

    scala> import io.finch._, io.finch.circe._
    
    scala> s(Input.post("/").withBody[Application.Json](Map("foo" -> "bar"))).awaitValueUnsafe()
    res2: Option[String] = Some({"foo":"bar"})