How to call rest API from another server scala

1.4k views Asked by At

I am new in scala. Currently, to create rest API I'm using the spray. Now I want to consume API from another server. I'm calling this API on every key-stroke from UI. I'm aborting the request using AbortController if the user keeps typing and the previous request is in pending status. To hit other server request, I'm using spray-client. Which goes something like this:

def completeService(completeRequest: CompleteRequest): Future[HttpResponse] = {

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

I'm able to access using this above code. And I'm getting the expected response. But the thing is time-consuming. It creates new TCP connection and communicates with the host, hits the API gives the response then terminates the connection. Here while terminating it sits idle for sometimes and didn't accept a new connection.

Is there any alternative way to do this?

1

There are 1 answers

2
igx On

You can create rest request using akka http client. you can see detailed example here