在播放器框架和scala Akka类型中的请求构建中将头设置为accept,将值设置为application/json

klsxnrf1  于 2022-11-06  发布在  Scala
关注(0)|答案(1)|浏览(126)

类包含到服务器的连接。

lazy val ConnectionFlow: Flow[HttpRequest, HttpResponse, Any] =
    Http().outgoingConnection(config.getString("host"), config.getInt("port"))

  lazy val AppService = new Service(config, ConnectionFlow)

服务.scala类

def Request(request: HttpRequest): Future[HttpResponse] =
    Source.single(request).via(ConnectionFlow).runWith(Sink.head)

//building Json request 
val reqJs = Json.obj("PARAMS" -> Json.obj("param1" -> value1))

Request(RequestBuilding.Post("/services/serviceName",reqJS).flatMap { response =>
// need response to be in JSobject format but the service returns application/xml format
7kqas0il

7kqas0il1#

如果我理解正确的话,您是在询问如何修改请求s.t.,它向服务器指示它期望JSON响应。
要做到这一点,您可以尝试

val builder = RequestBuilding.Post("/services/serviceName",reqJS)
builder.addHeader(Accept(MediaRange(`application/json`)))
// send out the request as you did

根据https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept,Accept Header只会显示客户端能够理解的内容类型,而服务器完全可以根据客户端的请求做出响应。也就是说,服务器可以根据客户端的要求做出任何响应,而不管客户端发送的是什么报头。

相关问题