0

I have an Akka-http 2 server to handle HTTP requests.

On either endpoint, I'm converting to case class A or B:

case class caseClassA(data: String, eventID: String)
case class caseClassB(data: String, otherData: JsObject)

I have a routes object like this:

   val routes = {
     logRequestResult("akka-http-microservice") {        
       pathPrefix("some_endpoint")
        (post & entity(as[caseClassA])) {
        Request =>
          val future = someFunction(Request)
            complete {
              future.map(result =>
                result
              )
            }
        } ~
        pathPrefix("other_endpoint") {
          (post & entity(as[caseClassB])) {
          Request =>
            val future = otherFunction(Request)
              complete {
                future.map(result =>
                  result
                ) 
              }
            }
          }
        }

The problem: Sometimes, when calling from an iOS app using Alamofire, I'm getting an error response for bad data to some_endpoint when making a request for other_endpoint with a caseClassB-convertible JSON object:

[DEBUG] [01/22/2016 16:30:16.350] [ReactiveKafka-akka.actor.default-dispatcher-23] [ActorSystem(ReactiveKafka)] akka-http-microservice: Response for
  Request : HttpRequest(HttpMethod(POST),http://192.168.2.141:9004/other_endpoint,List(Host: 192.168.2.141:9004, Connection: keep-alive, Accept: */*, User-Agent: myapp/myco.myapp (1; OS Version 9.2  (Build 13C75)), Accept-Language: en-US, zh-Hans-US;q=0.9, Accept-Encoding: gzip, compress;q=0.5),HttpEntity.Strict(application/json,ByteString(XXX, XXX, XXX, XXX, ETC <REPLACED FOR READABILITY, THIS WAS GOOD DATA>)),HttpProtocol(HTTP/1.1))
  Response: Complete(HttpResponse(200 OK,List(),HttpEntity.Strict(application/json,ByteString(XXX, XXX, XXX, XXX, ETC <REPLACED FOR READABILITY, THIS WAS GOOD DATA>)),HttpProtocol(HTTP/1.1)))
[DEBUG] [01/22/2016 16:30:21.347] [ReactiveKafka-akka.actor.default-dispatcher-5] [akka://ReactiveKafka/user/$a/flow-17-2-prefixAndTail] Cancelling akka.stream.impl.MultiStreamOutputProcessor$SubstreamOutput@454147bb (after: 5000 ms)
[DEBUG] [01/22/2016 16:30:26.259] [ReactiveKafka-akka.actor.default-dispatcher-7] [ActorSystem(ReactiveKafka)] akka-http-microservice: Response for
  Request : HttpRequest(HttpMethod(POST),http://192.168.2.141:9004/other_endpoint,List(Host: 192.168.2.141:9004, Connection: keep-alive, Accept: */*, User-Agent: myapp/myco.myapp (1; OS Version 9.2  (Build 13C75)), Accept-Language: en-US, zh-Hans-US;q=0.9, Accept-Encoding: gzip, compress;q=0.5),HttpEntity.Default(application/json,319,akka.stream.scaladsl.Source@c3e53bd),HttpProtocol(HTTP/1.1))
  Response: Rejected(List(MalformedRequestContentRejection(Object is missing required member 'eventID',Some(java.util.NoSuchElementException: key not found: eventID)), TransformationRejection(<function1>), MalformedRequestContentRejection(Unexpected end-of-input at input index 0 (line 1, position 1), expected JSON Value:

^
,None), TransformationRejection(<function1>)))

As you can see by missing eventID, this is trying to convert to caseClassA. Both requests in the output above show I'm trying to hit other_endpoint. When I call this endpoint from Postman with the same data, it works 100% of the time.

What gives?

olympia
  • 362
  • 2
  • 20
  • Is this going to be as simple as a missing set of curly braces after your _pathPrefix("some_endpoint")_ directive? – Richard Sitze Jan 23 '16 at 16:36
  • Yep. After much testing, it seems that was causing intermittent failure. It seems strange that sbt/IntelliJ had no problems with the bad syntax. Damn curly braces. – olympia Jan 25 '16 at 06:12

1 Answers1

0

As seen in the comments above, the issue was missing braces ({}) around the code for "some_endpoint". Here's the corrected code block:

val routes = {
  logRequestResult("akka-http-microservice") {        
    pathPrefix("some_endpoint") {
      (post & entity(as[caseClassA])) {
         Request =>
         val future = someFunction(Request)
         complete {
           future.map(result =>
             result
           )
         }
       }
     } ~
     pathPrefix("other_endpoint") {
       (post & entity(as[caseClassB])) {
         Request =>
         val future = otherFunction(Request)
         complete {
           future.map(result =>
             result
           ) 
         }
       }
     }
   }
olympia
  • 362
  • 2
  • 20