2
votes

I'm trying to do a simple REST API call using akka-http, circe and akka-http-json (akka-http-circe in particular).

import io.circe.generic.auto._

object Blah extends FailFastCirceSupport {
   //...
   val fut: Future[Json] = Http().singleRequest(HttpRequest(uri = uri))

I'm expecting akka-http-circe to figure out how to unmarshal a HttpResponse to my wanted type (here, just Json). But it doesn't compile.

So I looked at some documentation and samples around, and tried this:

val fut: Future[Json] = Http().singleRequest(HttpRequest(uri = uri)).flatMap(Unmarshal(_).to)

Gives: type mismatch; found 'Future[HttpResponse]', required 'Future[Json]'. How should I expose an unmarshaller for the fetch?

Scala 2.12.4, akka-http 10.0.10, akka-http-circe 1.18.0

1

1 Answers

2
votes

The working code seems to be:

val fut: Future[Json] = Http().singleRequest(HttpRequest(uri = uri)).flatMap(Unmarshal(_).to[Json])

When the target type is explicitly given, the compiler finds the necessary unmarshallers and this works. It works equally with a custom type in place of Json.

Further question: Is this the best way to do it?