我正在尝试使用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
。这不应该有预定义的实现吗?
它已实现,但仅限于2.5.1+
AFAIK,这只是从Play 2.6.x(特别是here, play-ws)开始实现的。
虽然Play 2.5.x有变通方法!
Source
,您需要将其封装在StreamBody
,as 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()))