如何在Akka Scala中合并两个表单数据?

时间:2018-09-17 20:33:40

标签: scala akka akka-http

我有一种方法,可以通过FormData将图片从客户端发送到CDN。代码:

def uploadToCDN(formData: Multipart.FormData): Future[HttpResponse] = {

implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
implicit val executionContext = system.dispatcher

Http().singleRequest(
  HttpRequest(
    method = HttpMethods.POST,
    uri = "http://cdn.example.com",
    entity = formData.toEntity(),
    protocol = HttpProtocols.`HTTP/1.1`))
}

如何将“ secret_key”:“ 12345678” 添加到从客户端收到的FormData中?

1 个答案:

答案 0 :(得分:1)

Multipart.FormData基本上由其部分组成。要连接两个FormData,您需要连接formdata部分并创建FormData的新实例:

val newFormData = 
  Multipart.FormData(
    Source.single(Multipart.FormData.BodyPart("secret_key", "12345678"))
      .concat(originalFormData.parts)
  )

另请参阅Multipart.FormData中的Scaladocs