【发布时间】:2017-01-13 02:41:26
【问题描述】:
我有一条消息。我使用akka-http。 而且我必须从另一台服务器调用此路由。
所以我必须用 HttpOriginRange.* 和 ch.megard.akka.http.cors.CorsDirectives 添加 cors。
答案是 json。 我想要一个 ContentType application/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")
}
}
}
}
}
}
}
如果我这样做,我有一个错误:Unexpected 's'
你能帮帮我吗?
【问题讨论】:
-
我认为发布完整的错误信息会有所帮助
-
这是我用 postman 作为回报的 uniq 消息。可悲的是,我的日志上没有任何内容:(
-
HttpResponse(entity = HttpEntity(ContentType(MediaTypes.
application/json), "success".getBytes)) ? -
Gaston,你的回答我有这个错误:HttpResponse 类型的表达式不符合预期的 server.Route 类型
-
我有一个带有此代码的后端点并且可以工作' complete{ HttpResponse(entity = HttpEntity(ContentType(MediaTypes.application/json), "success".getBytes)) }