play framework- how to send post request using java code in play framework

feikiss

Currently I'm switching to play framework to develop but I'm new to this wonderful framework. I just want to send a post request to remote server and get response.

If I use Jersey, it would be quite easy, just like this:

WebResource resource = client.resource("http://myfirstUrl");
 resource.addFilter(new LoggingFilter());
 Form form = new Form();
 form.add("grant_type", "authorization_code");
 form.add("client_id", "myclientId");
 form.add("client_secret", "mysecret");
 form.add("code", "mycode");
 form.add("redirect_uri", "http://mysecondUrl");       
 String msg = resource.accept(MediaType.APPLICATION_JSON).post(String.class, form);

and then I can get the msg which is what I want.

But in Play framework, I cannot find any libs to send such post request. I believe this should be a very simple feature and Play should have integrated it. I've tried to search and found most use case are about the Form in view leve. Could anyone give me some help or examples? Thanks in advance!

ysf

You can use Play WS API for making asynchronous HTTP Calls within your Play application. First you should add javaWs as a dependency.

libraryDependencies ++= Seq(
  javaWs
)

Then making HTTP POST Requests are as simple as;

WS.url("http://myposttarget.com")
 .setContentType("application/x-www-form-urlencoded")
 .post("key1=value1&key2=value2");

post() and other http methods returns a F.Promise<WSResponse> object which is something inherited from Play Scala to Play Java. Basically it is the underlying mechanism of asynchronous calls. You can process and get the result of your request as follows:

Promise<String> promise = WS.url("http://myposttarget.com")
 .setContentType("application/x-www-form-urlencoded")
 .post("key1=value1&key2=value2")
 .map(
    new Function<WSResponse, String>() {
        public String apply(WSResponse response) {
            String result = response.getBody();
            return result;
        }
    }
);

Finally obtained promise object is a wrapper around a String object in our case. And you can get the wrapped String as:

long timeout = 1000l;// 1 sec might be too many for most cases!
String result = promise.get(timeout);

timeout is the waiting time until this asynchronous request will be considered as failed.

For much more detailed explanation and more advanced use cases checkout the documentation and javadocs.

https://www.playframework.com/documentation/2.3.x/JavaWS

https://www.playframework.com/documentation/2.3.x/api/java/index.html

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

play framework- how to send post request using java code in play framework

From Dev

Send POST request with a body using scala and play framework

From Dev

WS Play Framework - How send File with post

From Dev

Play Framework: POST request with parameters

From Dev

Retrieve the request body string sent in POST request in play framework java

From Dev

play framework send file in HTTP request

From Dev

Play framework, Setting HTTP POST request headers

From Dev

Get data in the POST request body Play Framework

From Dev

Play framework 2.5 Json post request binder

From Dev

Play framework, Setting HTTP POST request headers

From Dev

Get data in the POST request body Play Framework

From Dev

Play framework 2.5 Json post request binder

From Dev

Play Framework Request is null

From Dev

Using a BodyParser with an authenticated request in Play 2.2.1 Framework

From Dev

Using a BodyParser with an authenticated request in Play 2.2.1 Framework

From Dev

Using POST routes parameters in Play Framework

From Dev

Using POST routes parameters in Play Framework

From Dev

how do I suspend a request in Play Framework?

From Dev

Play Framework Scala: How to Stream Request Body

From Dev

how do I suspend a request in Play Framework?

From Dev

Play Framework Scala: How to Stream Request Body

From Dev

How to upload multiple files in Play Framework using Java

From Dev

How to run code on startup in Play! framework 2.4

From Dev

For loop in Play Framework Java

From Dev

Java Play Framework flow

From Dev

Play framework and Java version

From Dev

Play Framework Forms (Java)

From Dev

Java Play Framework flow

From Dev

Chain Promises in Play Framework Using Java

Related Related

HotTag

Archive