nmat
nmat

Reputation: 7591

Akka HTTP client - Unmarshal with Play JSON

I am using Akka HTTP as a client to do a POST request and parse the answer. I am using Play JSON and I get the following compiler error:

could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.Unmarshaller[akka.http.javadsl.model.ResponseEntity,B]
[ERROR]       Unmarshal(response.entity).to[B].recoverWith {

This is the dependency I added to use Play JSON instead of Spray:

"de.heikoseeberger" %% "akka-http-play-json"

My class definition is:

class HttpClient(implicit val system: ActorSystem, val materializer: Materializer) extends PlayJsonSupport {

and the method definition is:

private def parseResponse[B](response: HttpResponse)(implicit reads: Reads[B]): Future[B] = {
  if (response.status().isSuccess) {
    Unmarshal(response.entity).to[B].recoverWith {
    ....

In the imports I have:

import play.api.libs.json._
import scala.concurrent.ExecutionContext.Implicits.global
import de.heikoseeberger.akkahttpplayjson.PlayJsonSupport._

It seems to me that I have the required implicits in scope. The Marshal part has a similar logic (but with Writes instead of Reads) and compiles fine. What am I missing?

Upvotes: 2

Views: 1464

Answers (1)

Jeffrey Chung
Jeffrey Chung

Reputation: 19527

Check your other imports. Based on the error message, it appears that you're using akka.http.javadsl.model.HttpResponse instead of akka.http.scaladsl.model.HttpResponse; PlayJsonSupport only supports the Scala DSL:

private def parseResponse[B](response: HttpResponse)(implicit reads: Reads[B]): Future[B] = ???
                                    // ^ this should be akka.http.scaladsl.model.HttpResponse

In other words, use

import akka.http.scaladsl.model._

instead of

import akka.http.javadsl.model._

Upvotes: 2

Related Questions