Re: [akka-user] Get whole HttpResponse body as a String with Akka-Streams HTTP

216 views
Skip to first unread message

Konrad Malawski

unread,
Aug 28, 2016, 7:16:36 PM8/28/16
to Akka User List
This is a double post. Well, triple even.

This was already answered here: 
and here:

Please ask in one place at the same time. It makes answering very hard to track if the same question is posted at the same time to many many places.


Please ask in one spot at once, this question was answered on akka-user already, very quickly after it was asked.

Please the ScalaDoc of value on Future:

If the future is not completed the returned value will be None. If the future is completed the value will be Some(Success(t)) if it contains a valid result, or Some(Failure(error)) if it contains an exception.

So you simply look into the Future before it had the chance to complete, so it's empty.

Instead use operations like map or onComplete to inspect it.


On Fri, Aug 26, 2016 at 1:44 PM, <luanxue...@gmail.com> wrote:
dear all:
         I  have written a akka http client example ,but  I can not get whole HttpResponse body as a String , my code is below:

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{HttpResponse, HttpRequest}
import akka.stream.ActorMaterializer
import scala.concurrent.duration._

import scala.util.{Failure, Success}

/**
* Created by luan on 8/26/16.
*/
object TestHttp {
def main(args: Array[String]) {
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
implicit val executionContext = system.dispatcher

//val url="http://cmdb.api.vip.com/webservice/domain_info/get_by_ip.do?name=bigdata-spark3-sd"
val url = "http://www.baidu.com"
println(url)
val responseFuture = Http().singleRequest(HttpRequest(uri = url))
var nodeCount: Int = 0
responseFuture.andThen {
case Success(resp: HttpResponse) => {
//println(resp.status.intValue())
//println(resp.status.defaultMessage())
//val aaaa = resp.entity.dataBytes.runFold(ByteString(""))(_ ++ _)
//println(aaaa.value.get.get.decodeString("UTF-8"))
//println(resp.entity.dataBytes.via(Framing.delimiter(ByteString("\n"),maximumFrameLength = 256,allowTruncation = true)).map(_.utf8String))
val entity = resp.entity.toStrict(5 seconds).map(_.data.decodeString("UTF-8"))
println(entity.value.getOrElse("none value"))
//nodeCount=JsonUtil.nodeCount(entity.value.get.get)
}
case Failure(ex:Exception) => {
println("http request error:"+ex.getMessage)
}
}
}
}

and  result is :

$ java -cp zabbix-rest-api-1.0-assembly.jar com.vip.bigdata.TestHttp
none value

who can tell me  why ? and how to write the code?

thanks very much

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+unsubscribe@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



--
Cheers,
Konrad 'ktoso' Malawski
Reply all
Reply to author
Forward
0 new messages