0

please help me, i want send json data to some API which use basic auth and i want catch respon from that API. this is my code:

@Inject
WSClient ws;
public Result testWS(){
JsonNode task = Json.newObject()
            .put("id", 123236)
            .put("name", "Task ws")
            .put("done", true);

WSRequest request = ws.url("http://localhost:9000/json/task").setAuth("user", "password", WSAuthScheme.BASIC).post(task);
return ok(request.tojson);

the question is how i get return from ws above and process it to json? because that code still error. i'm use playframework 2.5

rizrusn
  • 1
  • 1
  • 6

1 Answers1

0

.post(task) results in a CompletionStage<WSResponse>, so you can't just call toJson on it. You have to get the eventual response from the completion stage (think of it as a promise). Note the change to the method signature too.

import java.util.concurrent.CompletionStage;
import javax.inject.Inject;
import javax.inject.Singleton;
import com.fasterxml.jackson.databind.JsonNode;
import play.libs.Json;
import play.libs.ws.WSAuthScheme;
import play.libs.ws.WSClient;
import play.libs.ws.WSResponse;
import play.mvc.Controller;
import play.mvc.Result;
import scala.concurrent.ExecutionContextExecutor;

@Singleton
public class FooController extends Controller {

    private final WSClient ws;
    private final ExecutionContextExecutor exec;

    @Inject
    public FooController(final ExecutionContextExecutor exec,
                           final WSClient ws) {
        this.exec = exec;
        this.ws = ws;
    }

    public CompletionStage<Result> index() {
        final JsonNode task = Json.newObject()
                                  .put("id", 123236)
                                  .put("name", "Task ws")
                                  .put("done", true);

        final CompletionStage<WSResponse> eventualResponse = ws.url("http://localhost:9000/json/task")
                                                               .setAuth("user",
                                                                        "password",
                                                                        WSAuthScheme.BASIC)
                                                               .post(task);

        return eventualResponse.thenApplyAsync(response -> ok(response.asJson()),
                                               exec);
    }
}

Check the documentation for more details of working with asynchronous calls to web services.

Steve Chaloner
  • 8,162
  • 1
  • 22
  • 38
  • the colour of "map" and "toJson". and when i run it. show error: cannot find symbol symbol: method map((response)[...]Json)) location: variable eventualResponse of type java.util.concurrent.CompletionStage – rizrusn Apr 20 '16 at 08:18
  • Sorry, I wrote this from memory using the old `F.Promise` API in my head. I've updated the answer with the correct method call. – Steve Chaloner Apr 20 '16 at 08:32
  • there still an error, AsyncController : invalid method declaration; return type required – rizrusn Apr 21 '16 at 06:45