Strange behavior with akka-http and Future


(Bruno Manzo) #1

Hello, I am new to akka and akka http and need some help with Future serialization.

To keep things simple i have a server with the following endpoints:

         get {
            path("aa") {
              complete(Created -> "bar")
            }
          } ~
            get {
              path("bb") {
                complete(Created -> JObject( "id" -> JString("anything")))
              }
            } ~
            get {
              path("cc") {
                complete(Created -> Future {JObject( "id" -> JString("anything"))})
              }
            }

For the first two cases i am getting exactly what i want:

< HTTP/1.1 201 Created
< Server: akka-http/10.1.1
< Date: Wed, 27 Jun 2018 20:12:53 GMT
< Content-Type: text/plain; charset=UTF-8
< Content-Length: 3
< 
* Connection #0 to host localhost left intact
bar
< HTTP/1.1 201 Created
< Server: akka-http/10.1.1
< Date: Wed, 27 Jun 2018 20:12:55 GMT
< Content-Type: application/json
< Content-Length: 17
< 
* Connection #0 to host localhost left intact
{"id":"anything"}

But with the case with Future:

< HTTP/1.1 200 OK
< Server: akka-http/10.1.1
< Date: Wed, 27 Jun 2018 20:15:03 GMT
< Content-Type: application/json
< Content-Length: 166
< 
* Connection #0 to host localhost left intact
{"_1":{"intValue":201,"reason":"Created","defaultMessage":"The request has been fulfilled and resulted in a new resource being created.","allowsEntity":true},"_2":{}}

What I am missing here?

Thx


(Nafer Sanabria) #2

I recommend you to take a look at the future directives section https://doc.akka.io/docs/akka-http/current/routing-dsl/directives/future-directives/onComplete.html


(Bruno Manzo) #3

Thanks, now everything is working again, thx :)


(Arnout Engelen) #4

Because of GenericMarshallers.futureMarshaller you can also:

complete(Future { Created -> JObject( "id" -> JString("anything"))})

(adding https://github.com/akka/akka-http/pull/2095)