How to read a chunked encoded message as it comes in?

243 views
Skip to first unread message

John Kida

unread,
Nov 27, 2012, 5:03:42 PM11/27/12
to dispatc...@googlegroups.com
I am having alot of trouble getting dispatch 0.9.4 to read a chunked encoded as it comes in. The problem is that it seems to always wait for the complete message, getting a 200 OK, before it allows me to start reading the inputstream. For example in apache.commons i just did this

post = new PostMethod("http://www.myrestcall.com/callme")
inStream = post.getResponseBodyAsStream();
client.executeMethod(post);
var c = 0;
while ({c = inStream.read(); c != -1}) {
  print(c.asInstanceOf[Char])
}

Using apache http the above will start reading each character from the body as it comes in.

I am trying to do this same thing with dispatch.. here is what I have.

val subReply = Http(url("http://myrestcall") > as.Response(_.getResponseBodyAsStream()))
var read = 0;
for (c <- subReply) {
  while({read = c.read; read != -1}){
    print(c.asInstanceOf[Char])
  }
}

How can i start reading the input stream right away, and not have to wait for the entire connection to close and be finished?

Nathan Hamblen

unread,
Nov 27, 2012, 6:43:44 PM11/27/12
to dispatc...@googlegroups.com, John Kida

Xi Shen

unread,
Aug 20, 2016, 9:46:15 AM8/20/16
to Dispatch, jdk...@gmail.com
Hi n8han,

I tried as.stream.Lines, but I found the application never ends...it seems the Future is not properly resolved.

val f = Http(target > as.stream.Lines(l => {
    println(s"xxx${l.length}xxx")
}))

The "target" is a url pointing to www.google.com...so it is not really a stream response. I just want to test the idea. With the above code, the "println" function is executed, but the app will never end.

Aaron Hawley

unread,
Aug 31, 2016, 12:34:56 AM8/31/16
to Dispatch, jdk...@gmail.com
I tried as.stream.Lines, but I found the application never ends...it seems the Future is not properly resolved.

What version are you using of dispatch?

Application exits successfully for me with version 0.11.2:

object Main extends App {

  val target = url("http://www.google.com")
  val f = Http(target > as.stream.Lines(l => {
    println(s"xxx${l.length}xxx")
}))

  Await.ready(f, Duration.Inf)
  println("FINISHED")
}

Reply all
Reply to author
Forward
0 new messages