Scala Akka http发布请求,带有正文和喷雾json

问题描述 投票:0回答:1

[这是我目前的方法之一-

 val uri = "https://blahblah/blah/export"
  val body = Map("search" -> "search earliest=-2040h index=app_events sourcetype=kube:container:shop | search \"Error\"", "output_mode" -> "json")
  val entity = HttpEntity(ContentTypes.`application/json`, body)
  val request = HttpRequest(method = HttpMethods.POST, uri = uri, entity = entity)

我不完全确定自己是否正在以正确的方式这样做。我试图向包含正文的API发出发布请求,然后使用spray json将结果作为json字符串检索。目前,它不喜欢我的http实体(它已超载),而且我似乎无法弄清楚主体的实际位置。另外,我还在努力弄清楚如何以json字符串的形式获取响应。

scala http akka akka-http spray-json
1个回答
0
投票

您可以使用spray的prettyPrint在HttpEntity初始化中直接将您的正文格式化为json。

import akka.http.scaladsl.Http

...

val uri = "https://blahblah/blah/export"
val values = Map("search" -> "search value")
val request = HttpRequest(
  HttpMethods.POST,
  uri = uri,
  entity = HttpEntity(
    ContentTypes.`application/json`,
    values.toJson.prettyPrint
  )
)

Http().bindAndHandleAsync(request, "localhost", 9000)

无论如何,这只是一个模式。

© www.soinside.com 2019 - 2024. All rights reserved.