首页 > 解决方案 > 如何停止 akka http 服务器添加默认的 Content-type Header?

问题描述

我正在尝试处理我的一条路由中的请求,如果我没有在请求中传递任何Content-type默认情况下,akka 会将其设置为application/octet-stream。请在下面找到我的实现 -

import akka.actor.typed.ActorSystem
import akka.actor.typed.scaladsl.Behaviors
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{ContentTypes, HttpEntity}
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import scala.io.StdIn
object HttpServerContentTypeTest {
  def main(args: Array[String]): Unit = {
    implicit val actorSystem = ActorSystem(Behaviors.empty, "RandomNumbers")
    implicit val executionContext = actorSystem.executionContext
    val route = path("proxy"){
      Route { context =>
        println(context.request.entity.contentType)
        context.complete(HttpEntity(ContentTypes.`text/plain(UTF-8)`, ""))
      }
    }
    val bindingFuture = Http().newServerAt("localhost", 9000).bind(route)
    // press any key to stop the server
    StdIn.readLine()
    bindingFuture.flatMap(_.unbind()).onComplete(_ => actorSystem.terminate())
  }
}

我通过 Postman 进行了测试(请找到显示标题和使用的正文的屏幕截图)。我正在尝试发送带有非空请求正文的发布请求。另外我没有设置内容类型标题。我希望 akka-http 服务器内部不设置标头Content-type。有没有办法禁用这种默认行为?

显示所有请求标头的图像

显示请求正文的图像

标签: akka-http

解决方案


推荐阅读