Play framework make http request from play server to "somesite.com" and send the response back to the browser - scala

I'm developing an application using Play framework in scala. I have to handle the below use case in my application.
For a particular request from the browser to the play server the Play server should make an http request to some external server (for Eg: somesite.com) and send the response from this request back to the web browser.
I have written the below code to send the request to external serever in the controller.
val holder = WS.url("http://somesite.com")
val futureResponse = holder.get
Now how do I send back the response recieved from "somesite.com" back to the browser?

There's an example in the Play documentation for WS, under Using in a controller; I've adapted it to your scenario:
def showSomeSiteContent = Action.async {
WS.url("http://somesite.com").get().map { response =>
Ok(response.body)
}
}
The key thing to note is the idiomatic use of map() on the Future that you get back from the get call - code inside this map block will be executed once the Future has completed successfully.
The Action.async "wrapper" tells the Play framework that you'll be returning a Future[Response] and that you want it to do the necessary waiting for things to happen, as explained in the Handling Asynchronous Results documentation.

You may also be interested in dynamically returning the status and content type:
def showSomeSiteContent = Action.async {
WS.url("http://somesite.com").get().map { response =>
Status(response.status)(response.body).as(response.ahcResponse.getContentType)
}
}
Dynamic status could help if the URL/service you call fails to answer correctly.
Dynamic content type can be handy if your URL/service can return different content HTML/XML... depending on some dynamic parameter.

Related

How to call rest API from another server scala

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?
You can create rest request using akka http client. you can see detailed example here

scala return http request response of created object

I have an application with following back-end-technologies: scala/slick in playframework. Front- and back-end communicate via REST.
Now what I want to do is simply return a created/inserted (updated) row back to the front-end of my application. I thought about doing something like this:
def createClient = Action.async { implicit request =>
request.body.asJson.map(_.validate[ClientModel]) match {
case c: JsSuccess[ClientModel] =>
clientDTO.createClient(c.get).map{
cnt => Ok(Json.obj("id" -> cnt.id))
}.recover {
case e: Exception => BadRequest("Could ssnotsdreate client")
}
}
}
My code compiles but it gives me this error message while running:
XMLHttpRequest cannot load http://localhost:9002/clients. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 500.
I read about adding CORS to my application but would prefer to solve it otherwise. I thought there has to be a proper, elegant way to return a just created/inserted object back to the front-end, since it should be a core feature of any client-server communication.
I'm relatively new to scala, so please don't get hung up on my code and rather view it as pseudo code. This is a rather general question. Thank you!

How to make internal synchronous post request in Play framework and scala?

I'm new to Play and Scala. I'm trying to build an Application using Play and Scala. I need to make post call internally to get data from my server. But this should be synchronous. After getting the data from this post request, I need to send that data to front end. I've seen many resources but all are asynchronous. Please help me.
I'm fetching data from DB and then should return the data as response.
DB is at remote server not in the hosted server.
I think you should not block anyway.
def action = Action.async {
WS.url("some url")
.post(Json.toJson(Map("query"->query)))
.map { response =>
val jsonResponse = response.json
// in this place you have your response from your call
// now just do whatever you need to do with it,
// in this example I will return it as `Ok` result
Ok(jsonResponse)
}
}
Just map the result of your call and modify it staying in context of Future and use Action.async that takes a Future.
If you really want to block use Await.result(future, 5 seconds), importing
import scala.concurrent.duration._
import scala.concurrent.Await
See docs for Await here
All requests are asynchronous but nothing prevents you from waiting the response with await in your code.
val response = await(yourFutureRequest).body
The line written above will block until the future has finished.

Stopping Enumerators and Iteratees in PlayFramework when the request is closed

I am using enumerators in a Play Framework application. When a request is sent to a specific URI, I call a MongoService (using reactivemongo) that returns an enumerator and this result is then sent through an HTTP stream using Ok.chunked(stream), the problem is that if I close the browser while receiving this stream response, Play Framework keeps receiving data from the reactivemongo enumerator. How can I cancel the mongo enumerator when the client closes the browser?
EDIT:
Code example
The enumerator I get from reactivemongo:
def findStreamByCriteria(criteria: JsObject, limit: Int): Enumerator[E] = {
collection.find(criteria).cursor[E](readPreference = ReadPreference.primary).enumerate(limit)
}
Then, in my controller I have:
val stream = service.findStreamByCriteria(query)
Future {
Ok.chunked(stream)
}
The problem is that when the chunked request is cancelled, reactivemongo keeps sending data to the HTTP server.

Play Framework - Store Information About Current Request

In my play framework 2 application I'd like to have a log message with the request, response, and some details about the response - such as the number of search results returned from an external web call.
What I have now is a filter like this:
object AccessLog extends Filter {
import play.api.mvc._
import play.api.libs.concurrent.Execution.Implicits._
def apply(next: RequestHeader => Future[SimpleResult])(request: RequestHeader): Future[SimpleResult] = {
val result = next(request)
result map { r =>
play.Logger.info(s"Request: ${request.uri} - Response: ${r.header.status}")
}
result
}
}
At the point of logging, I've alread converted my classes into json, so it seems wasteful to parse the json back into objects so I can log information about it.
Is it possible to compute the number of search results earlier in the request pipeline, maybe into a dictionary, and pull them out when I log the message here?
I was looking at flash, but don't want the values to be sent out in a cookie at any cost. Maybe I can clear the flash instead. Buf if there's a more suitable way I'd like to see that.
This is part of a read-only API that does not involve user accounts or sessions.
You could try using the play.api.cache.Cache object if you can come up with a reproducible unique request identifier. Once you have logged your request, you can remove it from the Cache.