【问题标题】:PlayWS post multipart form dataPlayWS 发布多部分表单数据
【发布时间】:2016-11-08 14:39:17
【问题描述】:

我正在尝试使用 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()))

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

【问题讨论】:

  • 你能在你的代码中分享你的导入吗?我正在尝试做你正在做的事情,但我在为帖子制作源时遇到了麻烦,它不会告诉我创建一个可写的。使用 2.5.12

标签: scala playframework akka-stream


【解决方案1】:

已实现,但仅在 2.5.1+ 中实现

【讨论】:

    【解决方案2】:

    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()))

    【讨论】:

      猜你喜欢
      • 2015-10-31
      • 2019-11-19
      • 2017-06-06
      • 2016-11-11
      • 1970-01-01
      • 1970-01-01
      • 2014-08-06
      • 2016-02-12
      相关资源
      最近更新 更多