actually I am doing redirect from one play application to another play application, finally I receive response as Result object.. Below is the action in two applications. I am redirecting from apllication1 to application2. Application 2 will return JSON string, that I need to extract.
How can I retrieve JSON content from Result object?
Application1:
public static Result redirectTest(){
Result result = redirect("http://ipaddress:9000/authenticate");
/*** here I would like to extract JSON string from result***/
return result;
}
Application2:
@SecuredAction
public static Result index() {
Map<String, String> response = new HashMap<String, String>();
DemoUser user = (DemoUser) ctx().args.get(SecureSocial.USER_KEY);
for(BasicProfile basicProfile: user.identities){
response.put("name", basicProfile.firstName().get());
response.put("emailId", basicProfile.email().get());
response.put("providerId", basicProfile.providerId());
response.put("avatarurl", basicProfile.avatarUrl().get());
}
return ok(new JSONObject(response).toString());
}
This functions works fine for me.. Thanks to Mon Calamari
}
redirect
return results with error code 303, which cause the caller (browser) to do another request to the given url.What you need to do is actually proxying. Application1 should make a request to Application2, and handle the response.
Play have very nice Web Service API which allow doing this easily.
First i write this scala method to convert Enumerator[Array[Byte]] to Future[Array[Byte]]:
Then convert returned Future to Promise and finally get promise value:
You'll need to pass an instance of akka.stream.Materializer into JavaResultExtractor's getbody method.
Use Google Guice for Injecting at constructor level or declaration level.
and Further you can convert Result into String or any other type as required:
In the context of a controller method you could try:
This SO question may also help: JSON and Play
Use JavaResultExtractor, example:
Having a byte array, you can extract charset from Content-Type header and create java.lang.String:
Some of above code was copied from play.test.Helpers