Frecklefoot Frecklefoot - 1 year ago 40
Java Question

Can't get JSON from a GET request

New to Play! Framework and web development in general, I'm trying to do a simple REST GET to a web service and just get some straight-forward JSON in response. Typing the URL into a browser, I get a perfect response, with nicely formatted JSON. Calling it via code, it just blows up:

WS.WSRequest wsRequest = WS.url( serviceURL );
wsRequest.timeout( timeoutTime );
wsRequest.setHeader("Accept", "application/json");
wsRequest.headers.put( "Content-type","application/json" );
wsRequest.mimeType = "application/json";

WS.HttpResponse response = wsRequest.get();
String graphServiceResponse = response.getJson().toString();

Everything executes fine, until the last line where it throws an exception and errors out. I know I have what looks like a lot of redundant code; those are my attempts to fix it. Like I said, typing the "serviceURL" into a browser, it works fine.

Anyone know what I'm doing wrong? Thanks in advance!

Answer Source

Okay, solved this. Just omitted all the sets and such, added authentication and it worked perfectly. Weird.

   String stringResponse = "";
   try {
      // execute GET to graph service
      WS.WSRequest wsRequest = WS.url( serviceURL ).authenticate( USERNAME, PASSWORD );
      WS.HttpResponse response = wsRequest.get();
      stringResponse = response.getString();

      ... more cool stuff ...

Thanks for looking!