How to retrieve http get response as a complete json String in groovy using httpbuilder
With groovy.json.JsonOutput
:
HTTPBuilder http = new HTTPBuilder('http://date.jsontest.com/', ContentType.JSON);http.headers.Accept = ContentType.JSONhttp.parser[ContentType.JSON] = http.parser.'application/json'http.request(Method.GET) { response.success = { resp, json -> println json.toString() // Not valid JSON println JsonOutput.toJson(json) // Valid JSON println JsonOutput.prettyPrint(JsonOutput.toJson(json)) }}
Result:
{time=09:41:21 PM, milliseconds_since_epoch=1497303681991, date=06-12-2017}{"time":"09:41:21 PM","milliseconds_since_epoch":1497303681991,"date":"06-12-2017"}{ "time": "09:41:21 PM", "milliseconds_since_epoch": 1497303681991, "date": "06-12-2017"}