Convert HttpClient POST (name/value pairs) to Java WS Api

185 views
Skip to first unread message

jaybytez

unread,
Oct 21, 2014, 9:16:14 PM10/21/14
to play-fr...@googlegroups.com
I am doing a web service call via HttpClient in my Play application and am running into threading data issues that is causing major errors, data clashes in different requests, etc. So I want to convert my code to use the Play WS Api. What I want to understand is that the Api I call takes name/value pairs over a POST and returns values via name/value pairs. Would I use the Play WS submit as form to send my request and how would I receive those name/value pair responses?

Here is the general code for the HttpClient call:

public static Map<String, String> sendCCPayment(Payment payment, Map<String, String> paymentGatewayConfig) {
HttpPost post = new HttpPost(gatewayUrl);
HttpClient client = new DefaultHttpClient();
Map<String, String> responseProps = new HashMap<String, String>();
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

//Bind payment name value pairs for the POST Request
//The service takes name value pairs or xml

post.setEntity(new UrlEncodedFormEntity(data));

HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

String line = "";
while ((line = rd.readLine()) != null) {
//read data
}
} catch (IOException e) {
e.printStackTrace();
}
return responseProps;
}

Thanks

Will Sargent

unread,
Oct 22, 2014, 12:56:58 AM10/22/14
to play-fr...@googlegroups.com
Use 
WS.url(url).setContentType("application/x-www-form-urlencoded")
        .post(urlEncodedString);

and to decode, you would call

String body = response.getBody()
String urlDecodedBody = urlDecoder.decode(body, "UTF-8")



Will Sargent
Consultant, Professional Services
Typesafe, the company behind Play Framework, Akka and Scala


--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jaybytez

unread,
Oct 24, 2014, 12:05:45 AM10/24/14
to play-fr...@googlegroups.com
Two quick questions, I read through the WS documentation and the Async documenation (as I am upgrading from 2.2.4 to 2.3.5 for Play and am totally new to the functional concept).

1 - I wrote the WS request as you provided below with a .map function that calls the getBody and returns a Promise<String>, but I have no idea how to get the actual value out of the Promise wrapper and can't understand how to extract it based on the Async documentation.
2 - I have a method that does quite a bit of work sequentially and then fires off this WS request and then does some work based on the response values, am I going to get any type of performance or thread safety benefit by running it with the Play WS library?  Or is it better to take this larger methods and break them down into individual functions (meaning I take data from a form and then map it to the form string, post the WS, parse the results, look for errors, and pass back the results).  Is it inefficient from a functional perspective to do this all sequentially in a single method?  I was having threading clashes in this app with HttpClient from Apache.

Thanks...jay
Reply all
Reply to author
Forward
0 new messages