Pergunta

I'm new to playframework and to REST API. I want to send a POST request to REST API in a controller.

What is the best way to do it? Does play have a support for it or do I have to use a plugin?

Basically I want it to look like this:

  1. User submits a form.
  2. I load a form data in a controller.
  3. I send form data as a POST request
  4. Get response, do something with it and display result

So far I'm stuck at point 3, I have no idea how to do this.

Code to visualize what I have in mind:

public static Result processForm() {
    Form<FormData> myForm = Form.form(FormData.class).bindFromRequest();
    String text = myForm.get().text;

    //Send 'text' in a post request and get response

    text = doSomethingWithResponse(response);

    return ok(resultpage.render(text));
}

I don't think it matters but this is the API I want to use: http://open.xerox.com/Services/fst-nlp-tools/Pages/API%20Docs

Foi útil?

Solução

The following line of code sends a request and waits for a reponse:

WS.url(feedUrl).setHeader("Content-Type", "application/x-www-form-urlencoded").post("arg1=val1&arg2=val2").get().asJson();
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top