Converting WSResponse to Result

1,093 views
Skip to first unread message

Chun How Ng

unread,
Jan 24, 2017, 5:36:23 AM1/24/17
to Play Framework
Hi,

I have a WSResponse object from executing an HTTP call via WSClient's get() and it contains a HTML page.

I'm wondering Is there any solution in Java to convert play.libs.ws.WSResponse object to a play.mvc.Result

I have tried some Scala implementations based on this & this. They don't seem to work on me.


*I'm on Play v2.5.8 

Will Sargent

unread,
Jan 24, 2017, 12:43:00 PM1/24/17
to play-fr...@googlegroups.com
You can do this by mapping through the response using thenApply:

public CompletionStage<Result> index() {
    return ws.url(feedUrl).get().thenApply(response ->
        ok("Feed title: " + response.asJson().findPath("title").asText())
    );
}


--
Will Sargent
Engineer, Lightbend, Inc.


--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/386b89b0-bdf6-4e37-8d7a-c3f56095b32f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chun How Ng

unread,
Jan 24, 2017, 10:39:40 PM1/24/17
to Play Framework
Thanks, I get that. But I guess what I'm trying to achieve is to display/render the response (html page) as it is in the browser.

ie: based on the code stub you gave, assuming feedUrl = "https://playframework.com/", I want to render this page (the response) when index() from the controller is called.



On Wednesday, 25 January 2017 01:43:00 UTC+8, Will Sargent wrote:
You can do this by mapping through the response using thenApply:

public CompletionStage<Result> index() {
    return ws.url(feedUrl).get().thenApply(response ->
        ok("Feed title: " + response.asJson().findPath("title").asText())
    );
}


--
Will Sargent
Engineer, Lightbend, Inc.


On Tue, Jan 24, 2017 at 2:36 AM, Chun How Ng <chunh...@gmail.com> wrote:
Hi,

I have a WSResponse object from executing an HTTP call via WSClient's get() and it contains a HTML page.

I'm wondering Is there any solution in Java to convert play.libs.ws.WSResponse object to a play.mvc.Result

I have tried some Scala implementations based on this & this. They don't seem to work on me.


*I'm on Play v2.5.8 

--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.

Will Sargent

unread,
Jan 25, 2017, 8:49:26 PM1/25/17
to play-fr...@googlegroups.com
I don't see the problem -- in that case you'd render new Html(response.getBody()) instead of the string.

--
Will Sargent
Engineer, Lightbend, Inc.


To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/a055a389-48cc-4fe9-8843-407ec49273fd%40googlegroups.com.

Greg Methvin

unread,
Jan 25, 2017, 10:28:30 PM1/25/17
to play-framework
If you want a 2.5.x version of the code in #4787, it would look something like this:
def responseToResult(response: WSResponse): Result = {
val headers = response.allHeaders map { h => (h._1, h._2.head) }
val entity = HttpEntity.Strict(
ByteString(response.body.getBytes),
response.allHeaders.get("Content-Type").map(_.head)
)
Result(ResponseHeader(response.status, headers), entity)
}
You also might want to consider using ws.stream() and HttpEntity.Streamed if you're dealing with large amounts of data you don't want to read into memory on the server.


For more options, visit https://groups.google.com/d/optout.



--
Greg Methvin
Tech Lead - Play Framework

Reply all
Reply to author
Forward
0 new messages