Http4s: Tips and Tricks

Http4s: Tips and Tricks

·

1 min read

Get the text body of a response with EntityDecoder

If you've got a request like this:

val postRequest: Task[Request[Task]] = POST(
                                     payload,
                                     uri
                                   )

and you just want the body as text (e.g. for debug), use:

postRequest.map { req =>
  client.run(req).use {
    case Status.Successful(resp) =>
      EntityDecoder.decodeText(resp).flatMap { respText =>
        logger.info(s"The body of the response as text is: " + respText)
      }
  }
}