PlayWS发布多部分表单数据

问题描述 投票:3回答:2

我正在尝试使用PlayWS将文件和一些数据发布到Mailgun,但我收到此错误:

Cannot write an instance of akka.stream.scaladsl.Source[play.api.mvc.MultipartFormData.Part[akka.stream.scaladsl.Source[akka.util.ByteString, Any]], Any] to HTTP response. Try to define a Writeable[akka.stream.scaladsl.Source[play.api.mvc.MultipartFormData.Part[akka.stream.scaladsl.Source[akka.util.ByteString, Any]], Any]]

代码如下所示:

def ws(url: String) = 
  wsClient.url(s"${url}").withAuth("api", apiKey, WSAuthScheme.BASIC)

ws(url).post(Source(
  FilePart("test", "test.txt", Option("text/plain"), FileIO.fromFile(file)) :: 
  DataPart("key", "value") :: 
  List()))

我知道错误要求我做什么,但我不知道如何在这种情况下为Writable实现Source。这不应该有预定义的实现吗?

scala playframework akka-stream
2个回答
1
投票

它已实现,但仅限于2.5.1+


1
投票

AFAIK,这只是从Play 2.6.x(特别是here, play-ws)开始实现的。

虽然Play 2.5.x有变通方法!

As documented here

  • 要流式传输Source,您需要将其封装在StreamBodyas in中:

val wsResponse: Future[WSResponse] = ws.url(url) .withBody(StreamedBody(largeImageFromDB)).execute("PUT")

ws.url(url).post(Source(FilePart("hello", "hello.txt", Option("text/plain"), FileIO.fromFile(tmpFile)) :: DataPart("key", "value") :: List()))

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