json and cors with akka http



我有一条帖子消息。我使用 akka-http。我必须从另一台服务器调用此路由。

所以我必须用 HttpOriginRange.* 和 ch.megard.akka.http.cors.CorsDirectives 添加 cors。

答案是 json。我想要一个ContentType应用程序/json

我该怎么做?

这是我的路线代码:

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")
                        }
                    }
                }
            }
        }

我试过这个:

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")
                    }
                }
            }
        }
    }
   }
}

如果我这样做,我有一个错误:意外的"s"

你能帮我吗?

如果你尝试

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")
          }
        }
      }
    }
  }
}

最新更新