0

I am new to scala and spray. I am able to abort request from reactJS. And it shows in network tab of browser console that the request is cancelled. But from scala it is not aborting. In logs i can see api is getting hitted. For Rest API I am using spray in scala. Here is my reactJS code:

    new Promise((accept, _reject) => {
    fetch("/api/complete", {
      method: "post",
      signal: controller.signal,
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json"
      },
      body: JSON.stringify(requestBody)
    })

Ans here is my scala code:

pathPrefix("complete") {
    post {
        entity(as[completeRequest]) { completeRequest =>
            complete {
                    completeService()
            }
        }
    }
}

def completeService(): Future[HttpResponse] = {

    val pipeline: HttpRequest => Future[HttpResponse] = sendReceive ~> unmarshal[HttpResponse]
    val response: Future[HttpResponse] = pipeline(Post(someremoteUrl.concat("complete"), botCompleteRequest)
      ~> addHeader("demo", "test"))
    response
}

So how to abort this complete request when it is aborted from reactJS/promise

dharmesh singh
  • 101
  • 1
  • 6

1 Answers1

0

Short answer: You don't.

It does matter if it is React (or Angular, or jQuery) on the client-side and it does not matter what is on the server-side (scala, PHP, .NET).

What matters is the HTTP between client and server.

In HTTP you can't "catch" a request that is already sent. The abort function in js/browser pretty much only means that it will ignore the response. Once a request is sent and is in the network, it will hit the server, and the server will process it. The server never gets notified that the client canceled the request.

This question and the answer cover the topic quite well: https://softwareengineering.stackexchange.com/questions/362187/can-a-caller-abort-an-execution-of-code-invoked-by-http-request

Moritz
  • 549
  • 5
  • 13
  • I was just browsing I found this https://doc.akka.io/docs/akka-http/current/server-side/graceful-termination.html and https://doc.akka.io/docs/akka/current/stream/operators/Sink/cancelled.html – dharmesh singh Mar 04 '20 at 16:40
  • @dharmeshsingh This is documenation for `akka-streams`, not `akka-http` (known before as `spray`). Yes, indeed - you can cancel stream execution, but it has nothing in common with Future. Future can not be canceled after it's created. – Ivan Kurchenko Mar 04 '20 at 16:52
  • one which I mention Future[HttpResponse] is giving problems. Then I will have to come up with alternate of Future[HttpResponse] But spray-client or that pipeline method returns Future[HttpResponse]. Since I have to hit some other server get response thats y i'm using that – dharmesh singh Mar 04 '20 at 17:39
  • The problem that remains is the cancellation of the http request. You would have to send another http request to the server telling the server that you want to cancel the first request. – Moritz Mar 05 '20 at 13:45
  • So is there any sample example for this to do? or some example I can refer – dharmesh singh Mar 05 '20 at 18:01
  • Not that I am aware of. Just make sure your React application is not sending any requests that should not arrive at the server. – Moritz Mar 06 '20 at 12:51