2017-01-12 3 views
0

Ich habe eine Post-Nachricht. Ich benutze akka-http. Und ich muss diese Route von einem anderen Server anrufen.json und cors mit akka http

Also muss ich cors mit HttpOriginRange. * Mit ch.megard.akka.http.cors.CorsDirectives hinzufügen.

Die Antwort ist ein JSON. Und ich möchte eine ContentType-Anwendung haben/JSON

Wie kann ich das tun?

Hier ist meine Route Code:

settings = CorsSettings.defaultSettings.copy(allowGenericHttpRequests = true, allowCredentials = false, allowedOrigins = HttpOriginRange.*) 
val route: Route = 
     handleRejections(CorsDirectives.corsRejectionHandler) { 
      cors(settings) { 
       handleRejections(RejectionHandler.default) { 
        post { 
         authenticated(doAuthApp) { app => 
          complete("success") 
         } 
        } 
       } 
      } 
     } 

ich das versucht:

val route: Route = 

    handleRejections(CorsDirectives.corsRejectionHandler) { 
     cors(WebServer.settings) { 
      handleRejections(RejectionHandler.default) { 
       (decodeRequest & encodeResponse) { 
        mapResponseEntity(_.withContentType(ContentTypes.`application/json`)) { 
       post { 
        authenticated(doAuthApp) { app => 
         complete("success") 
        } 
       } 
      } 
     } 
    } 
    } 
} 

Wenn ich tun, dass ich einen Fehler haben: Unerwartete

's'

Könnten Sie mir bitte helfen ?

+0

versuchen denke ich die komplette Fehlermeldung veröffentlichen würde helfen –

+0

Dies ist die uniq Nachricht i mit Postbote im Gegenzug haben. Nichts auf meiner Protokolle traurig :( –

+0

HttpResponse (entity = HttpEntity (ContentType (MediaTypes.application/json), "Erfolg" .getBytes))? – gaston

Antwort

0

Wenn Sie

var settings = CorsSettings.defaultSettings.copy(allowGenericHttpRequests = true, allowCredentials = false, allowedOrigins = HttpOriginRange.*) 

val route = 
pathPrefix("testEndpoint") { 
    handleRejections(CorsDirectives.corsRejectionHandler) { 
    cors(settings) { 
     handleRejections(RejectionHandler.default) { 
     post { 
      entity(as[String]) { 
      app => 
       complete("success") 
      } 
     } 
     } 
    } 
    } 
}