简体   繁体   中英

Sending a post request to REST API from controller in Playframework with Java

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

以下代码行发送一个请求并等待响应:

WS.url(feedUrl).setHeader("Content-Type", "application/x-www-form-urlencoded").post("arg1=val1&arg2=val2").get().asJson();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM