首页 > 解决方案 > akka-http 网络::err_incomplete_chunked_encoding 200 (ok)

问题描述

我正在尝试向浏览器发送一个大/通常很慢的响应,以使用 Akka-http 呈现到一个 excel 文件中:即:

在 ui 代码中

$http({
  method : "post",
  url: "myUrl",
  data: "large amount of request data to run a api call",
  headers: {
    'Content-type': 'application/json',
    'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  },
  responseType: 'arraybuffer'
}).then
  (function mySuccess(response) {
    var blob = new Blob([response.data], type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    savesAs(blob, "excel.xlsx")
}) 

缓慢的 api 运行,我尝试将数据分块,即:

path(myPath) {
  post {
    entity(as[MyPredicates]) { entity =>
      val slowApi = Future[List[Results]] = runSlowApi(entity)
      val excel = slowApi.map(data => generateExcel(data)).toByteArray
      val source = Source.fromFuture(excel).map(ByteString.apply)
      val chunkStream = source.via(new Chunker(chunkSize = 8192))
      chunkStream.keepAlive(1.second, () => TextMessage.Strict("ping"))
      complete(HttpEntity(MediaTypes.`application/vnd.openxmlformats-officedocument.spreadsheetml.sheet`, chunkStream)
  }
}

对于少于 60 秒的任何事情,这都可以正常工作,但是在 60 秒后它总是会失败并显示

akka-http 网络::err_incomplete_chunked_encoding 200 (ok)

在浏览器上。

我已经尝试过withRequestTimeOut//并在客户端上设置了超时但没有任何效果withRequestTimeouResponsewithoutSizeLimit

标签: angularjsscalaakkaakka-streamakka-http

解决方案


如果代码使用它会更好responseType: 'blob'

$http({
  method : "post",
  url: "myUrl",
  data: "large amount of request data to run a api call",
  headers: {
    'Content-type': 'application/json',
    'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  },
  ̶r̶e̶s̶p̶o̶n̶s̶e̶T̶y̶p̶e̶:̶ ̶'̶a̶r̶r̶a̶y̶b̶u̶f̶f̶e̶r̶'̶
  responseType: 'blob'
}).then
  (function mySuccess(response) {
    ̶v̶a̶r̶ ̶b̶l̶o̶b̶ ̶=̶ ̶n̶e̶w̶ ̶B̶l̶o̶b̶(̶[̶r̶e̶s̶p̶o̶n̶s̶e̶.̶d̶a̶t̶a̶]̶,̶ ̶t̶y̶p̶e̶:̶ ̶'̶a̶p̶p̶l̶i̶c̶a̶t̶i̶o̶n̶/̶v̶n̶d̶.̶o̶p̶e̶n̶x̶m̶l̶f̶o̶r̶m̶a̶t̶s̶-̶o̶f̶f̶i̶c̶e̶d̶o̶c̶u̶m̶e̶n̶t̶.̶s̶p̶r̶e̶a̶d̶s̶h̶e̶e̶t̶m̶l̶.̶s̶h̶e̶e̶t̶'̶)̶;̶
    var blob = response.data;
    savesAs(blob, "excel.xlsx")
}) 

推荐阅读