Apache pekko ( akka http ) - Extracted string body from request doesn't have quote

113 Views Asked by At

I'm POCing pekko http ( akka http ) for my next project. I extracted json string from request body entity but it doesn't have quote on string value.

Code snippet on route dsl :

  post {
    path("account") {
      extract(_.request) { request =>
        val body = Await.result(request.entity.toStrict(1.second).map(_.data.utf8String), 1.second)
        println(body)
        complete(...)
      }
    }
  }

request : curl -X POST http://localhost:9090/account -d '{"account":{"name":"TESTE"}}'
extracted json string : {account:{name:TESTE}}

Anybody know how to extract request body entity without removing quotes ?

1

There are 1 best solutions below

5
earthling paul On

There is nothing stopping you to do it this way, however blocking with Await is not a good practice when using akka-http. This also extracts the raw JSON string without removing the quotes:

  val jsonRaw: Route =
  path("jsonRaw")(
    post(
      entity(as[String]) {
        json =>
          println("JSON raw: " + json)
          complete(StatusCodes.OK)
      }
    )
  )

Working example: https://github.com/pbernet/akka_streams_tutorial/blob/70e52e6e769a2779f1a6de744aab3cf4f16a3835/src/main/scala/akkahttp/SampleRoutes.scala#L121

To inspect the request on the server you may set these parameters in the application.conf in order to detect env issues.

pekko {
  loglevel = "DEBUG"
  http.server.log-unencrypted-network-bytes = 100
  ...
  }