Scala dispatch stream response line by line

可紊 提交于 2019-12-14 01:23:48

问题


I am attempting to use Scala dispatch but been both a Scala neewbie and the face that Dispatch api is symbol crazy Im confused on how I can stream a large http response and process it line by line. Any help would be appreciated.

Cheers, Chris.

Note:

This isn't working for me:

Http(url(Config.publisherUrl) > as.stream.Lines(line => println(line)))

The lines are never printed.

Edit:

The lines were being printed, but only when there was more than one line in the response. The issue seems to be that I can stream the data line by line, but the very last line is omitted.


回答1:


I had the same problem. What you could do. I read it as input stream, then converted it to Akka stream and returned source.

import akka.stream.scaladsl.{Source, StreamConverters}
  val futureStream = Http(url(urlString) > as.Response(_.getResponseBodyAsStream))
        futureStream.map { inputStream =>
          val source = () => inputStream
          StreamConverters.fromInputStream(source)
        }

It worked for me.



来源:https://stackoverflow.com/questions/17606186/scala-dispatch-stream-response-line-by-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!