JSON format results from HTTPBuilder GET changed between 0.5.2 and 0.6

Robert picture Robert · Sep 11, 2013 · Viewed 7k times · Source

We recently updated dependencies of a REST integration test which uses Groovy, HTTPBuilder, JSONAssert and Junit. When we went from HTTPBuilder 0.5.2 to 0.6 many of our tests failed.

We found that the format of the response changed due to a new "feature" in HTTPBuilder that provides "Automatic response parsing for registered content types".

Old, (0.5.2) format, expected response:

[ { "name":"Portfolio_REST_Test01", "description":"", "referenceValueType":"Net Value", "unitType":"SHARES", "tags":[] } ]

New (0.6.2) format of response:

[{tags=[], referenceValueType=Net Value, unitType=SHARES, description=, name=Portfolio_REST_Test01}]

The problem arises when JSONAssert attempts to parse a named value where the value is an empty string, see "Description" in the examples above. JSONAssert is expecting a character to follow the equals-sign, not a comma, and throws an exception when encountered.

Answer

Robert picture Robert · Sep 11, 2013

Digging further into this we found changes that must be made to HTTPBuilder to continue to get the unformatted responses 0.5.2 would provide.

Old Groovy code snip:

  // perform a GET request, expecting JSON response data
  http.request(Method.GET, ContentType.JSON) { req ->

     uri.path = restPath;
     requestContentType = JSON;
     uri.query = mapQuery;
     headers.Accept = 'application/json';

     // response handler for a successful response code:
     response.success = { resp, json ->
   // Check returned status'
   assertResp(resp, expStatus);
   return json.toString();
     }
     // Check returned status' for the 400's and 500's
     response.failure = { resp ->
        // Check returned status'
        assertResp(resp, expStatus);
     }

The HTTPBuilder documentation indicates automatic formatting will be used for known types like JSON and XML. In order to turn this feature off one must specify a ContentType of TEXT in the http.request. But when this is done the "json" object's toString value (within the response.success closure) no longer returns the returned JSON values. These values are found in the "text" property.

So the final code looks like this:

  // perform a GET request, expecting JSON response data
  http.request(Method.GET, ContentType.TEXT) { req ->

     uri.path = restPath;
     requestContentType = JSON;
     uri.query = mapQuery;
     headers.Accept = 'application/json';

     // response handler for a successful response code:
     response.success = { resp, json ->
   // Check returned status'
   assertResp(resp, expStatus);
   return json.text;
     }
     // Check returned status' for the 400's and 500's
     response.failure = { resp ->
        // Check returned status'
        assertResp(resp, expStatus);
     }

Hopefully if someone encounters this same issue, they'll consult Stack Overflow and they'll find these notes helpful. Best Regards