# Could not get the result from an HTTPS JSON Call with Bearer

**URL:** <https://discuss.akka.io/t/could-not-get-the-result-from-an-https-json-call-with-bearer/9498>\
**Category:** Akka Libraries\
**Tags:** scala\
**Created:** [February 7, 2022, 5:30pm UTC](https://discuss.akka.io/t/could-not-get-the-result-from-an-https-json-call-with-bearer/9498 "2022-02-07T17:30:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ozcano1](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/ozcano1/32/2679_2.png) [@ozcano1](https://discuss.akka.io/u/ozcano1)\
**Post date:** [February 7, 2022, 5:30pm UTC](https://discuss.akka.io/t/could-not-get-the-result-from-an-https-json-call-with-bearer/9498/1 "2022-02-07T17:30:15Z")

</div>

Hello

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

import akka.actor.typed.ActorSystem  
import akka.actor.typed.scaladsl.Behaviors  
import akka.http.scaladsl.Http  
import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpMethods, HttpRequest}  
import akka.http.scaladsl.model.headers.RawHeader  
import org.slf4j.LoggerFactory  
import scala.util.{Failure, Success}

object Main2 {  
val dataAsJson=“SOME JSON DATA”

val log = LoggerFactory.getLogger(“Main”)

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()  
}  
}

---

<div class="post-metadata">

**Author:** ![pbernet](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/pbernet/32/370_2.png) [@pbernet](https://discuss.akka.io/u/pbernet)\
**Post date:** [February 9, 2022, 5:01pm UTC](https://discuss.akka.io/t/could-not-get-the-result-from-an-https-json-call-with-bearer/9498/2 "2022-02-09T17:01:12Z")

</div>

I guess you need to consume the response entity at the correct position.  
In a similar situation sth like this worked for me:

```auto
  case class Ctx(title: String, personsFound: List[String] = List.empty, content: String)

  def fetchContent(ctx: Ctx): Future[Ctx] = {
    logger.info(s"About to read `extract` from Wikipedia entry with title: ${ctx.title}")
    val encodedTitle = URLEncoder.encode(ctx.title, "UTF-8")

    val requestURL = s"https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exlimit=max&explaintext&exintro&titles=$encodedTitle"
    Http().singleRequest(HttpRequest(uri = requestURL))
      // Consume the streamed response entity
      // Doc: https://doc.akka.io/docs/akka-http/current/client-side/request-level.html
      .flatMap(_.entity.toStrict(2.seconds))
      .map(_.data.utf8String.split("\"extract\":").reverse.head)
      .map(content => ctx.copy(content = content))
  }

```

Hope that helps  
Paul

---

<div class="post-metadata">

**Author:** ![ozcano1](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/ozcano1/32/2679_2.png) [@ozcano1](https://discuss.akka.io/u/ozcano1)\
**Post date:** [February 10, 2022, 1:45pm UTC](https://discuss.akka.io/t/could-not-get-the-result-from-an-https-json-call-with-bearer/9498/3 "2022-02-10T13:45:17Z")

</div>

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")
  }

```

OUTPUT:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/discuss_lightbend/original/2X/e/e3b3ad448172fc3d0e4276c988b44995b6d87d4a.png)

---

<div class="post-metadata">

**Author:** ![pbernet](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/pbernet/32/370_2.png) [@pbernet](https://discuss.akka.io/u/pbernet)\
**Post date:** [February 10, 2022, 2:00pm UTC](https://discuss.akka.io/t/could-not-get-the-result-from-an-https-json-call-with-bearer/9498/4 "2022-02-10T14:00:14Z")

</div>

Sorry, I forgot to mention that you need to provide a valid value for the `titles` attribute.  
In a Browser:

```auto
https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exlimit=max&explaintext&exintro&titles=Adam

```

this yields a JSON with an `extract` attribute.

---

<div class="post-metadata">

**Author:** ![ozcano1](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/ozcano1/32/2679_2.png) [@ozcano1](https://discuss.akka.io/u/ozcano1)\
**Post date:** [February 10, 2022, 5:04pm UTC](https://discuss.akka.io/t/could-not-get-the-result-from-an-https-json-call-with-bearer/9498/5 "2022-02-10T17:04:58Z")

</div>

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}")
```

---

<div class="post-metadata">

**Author:** ![pbernet](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/pbernet/32/370_2.png) [@pbernet](https://discuss.akka.io/u/pbernet)\
**Post date:** [February 10, 2022, 5:59pm UTC](https://discuss.akka.io/t/could-not-get-the-result-from-an-https-json-call-with-bearer/9498/6 "2022-02-10T17:59:27Z")

</div>

This:

> <https://gist.github.com/pbernet/dc26109e9e1f562d68ea69fbd70d7347>

yields:

```auto
...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...

```

Regards  
Paul

---

<div class="post-metadata">

**Author:** ![ozcano1](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/ozcano1/32/2679_2.png) [@ozcano1](https://discuss.akka.io/u/ozcano1)\
**Post date:** [February 11, 2022, 9:31am UTC](https://discuss.akka.io/t/could-not-get-the-result-from-an-https-json-call-with-bearer/9498/7 "2022-02-11T09:31:46Z")

</div>

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”

libraryDependencies ++= Seq(  
“com.typesafe.akka” %% “akka-actor-typed” % AkkaVersion,  
“com.typesafe.akka” %% “akka-stream” % AkkaVersion,  
“com.typesafe.akka” %% “akka-http” % AkkaHttpVersion,  
“com.typesafe.akka” %% “akka-slf4j” % AkkaVersion,  
“ch.qos.logback” % “logback-classic” % “1.2.10” % Runtime  
)
