I am trying to call a secure service using HTTP POST with JSON format (in and out). I do not get any error but I have not been able to write the result to log or to terminal to see it. Would you please help me? I pasted the code at the bottom.
Regards, Ozcan
NOT : I have successfully called the servis using c#, postman and advanced rest client
val request = HttpRequest(
method = HttpMethods.POST,
uri = “SOME HTTPS SERVICE”,
entity = HttpEntity(
ContentTypes.application/json,
dataAsJson
)
).withHeaders(RawHeader(“Authorization”, “Bearer SOME TOKEN”))
def simpleRequest() = {
implicit val system = ActorSystem(Behaviors.empty, “SingleRequest”)
implicit val executionContext = system.executionContext
log.info("request {}", request)
val responseFuture = Http().singleRequest(request)
responseFuture
.onComplete {
case Success(res) => println(res.toString()) //THIS PART IS NOT WORKING
case Failure(_) => sys.error("something wrong")
}
/*
for(response <- responseFuture) {
log.info("response received {}", response)
log.info("notified about EC Failure")
}
*/
//responseFuture.flatMap(_.entity.toStrict(5 seconds)).map(_.data.utf8String).foreach(println)
}
def main(args: Array[String]): Unit =
{
simpleRequest()
}
}
Hello
Thanks for the answer. Sorry! I am new to scala. I tried using and calling the code you provided as it is as seen below. It runs, but still nothing is printed to the terminal when future is completed. Probably I do not know how to use the Futures. I know that I am making a simple mistake but could not see it myself. I will appreciate your helps:
val personsFound: List[String] = List.empty
val ctx = Ctx("anytitle", personsFound,"")
val responseFuture:Future[Ctx] = fetchContent(ctx)
responseFuture
.onComplete {
case Success(res) => logger.info(s"Done with title: ${res.content}")
case Failure(_) => logger.info(s"Done with title err") //sys.error("something wrong")
}
Hello
It is not related what is returned or not. Even this simple code does not print anything. I am just trying to print “IT IS OK” to terminal assigning a value to var result inside the onComplete. But it does not work. I am simply missing something about how Futures work, but what?
var result =""
responseFuture
.onComplete {
case Success(res) => result="IT IS OK"
case Failure(_) => logger.info(s"Done with title err") //sys.error("something wrong")
}
logger.info(s"A title: ${result}")
...About to read `extract` from Wikipedia entry with title: Adam
...Title: Adam yields content: "Adam is the name given in Genesis 1-5 to the first human...
Hello
Thank you for your responses. I think what happens is when I run it in the İntelliJ terminal or in windows command prompt in does not work. But when I run in the İntelliJ sbt shell, it works.
Many thanks
Kind Regards
Ozcan
NOTE:
Java version is 16.0.2
Build.sbt is
version := “0.1”
scalaVersion := “2.13.8”
val AkkaVersion = “2.6.18”
val AkkaHttpVersion = “10.2.7”