title: Groovy RestClient not using correct response handler for Content-Type: application/json?
body: <p>I'm using Groovy's <a href="https://github.com/jgritman/httpbuilder/blob/855e1784be8585de81cc3c99fd19285798c7bc4f/src/main/java/groovyx/net/http/RESTClient.java" rel="nofollow">RestClient.java</a> to POST some plain text to a server, the response I get back is JSON but I'm having some issues figuring out why the response is being parsed into a <a href="http://docs.oracle.com/javase/7/docs/api/java/io/StringReader.html" rel="nofollow">StringReader</a> object instead of a <a href="http://docs.groovy-lang.org/latest/html/gapi/groovy/json/JsonSlurper.html" rel="nofollow">JsonSlurper</a> object.</p>
  
  <p>From what I understand the response body should be parsed into an object based on the content type in the response header, the response header I get back is <code>application/json</code> but it seems like the it's defaulting to and parsing the response into a <code>StringReader</code> object. I'm not sure if I'm defining my headers incorrectly or whether I need to specify my own success handler?? I have other methods that use the POST method and work fine, however they are posting XML and expecting XML in response so I think the headers work out fine in that instance.</p>
  
  <p>Below is how I'm attempting it and my logs, as you can see the response headers <code>Content-Type</code> is <code>application/json</code> so I'm baffled as to why <code>restClientResponse.getData()</code> is a <code>StringReader</code> object instead of a <code>JsonSlurper</code> object</p>
  
  <pre><code>def restClient = new RESTClient("http://this.doesnt.matter", 'text/plain')
  
  restClient.setProxy("someproxy", 8080, "http")
  restClient.setHeaders(Accept: "application/json")
  
  def restClientResponse = restClient.post(body: "requestPayload:ACCOUNTPROFILE")
  log.info(restClientResponse.getData())
  log.info("What is in StringReader")
  
  //Printing out what is in StringReader Object
  def reader = restClientResponse.getData()
  StringBuilder builder = new StringBuilder()
  char[] characters = new char[1000]
  builder.append(characters, 0, reader.read(characters, 0, characters.length))
  log.info(builder.toString())
  
  // Printing out each header in the response
  restClientResponse.getAllHeaders().each {
      log.info("HEADER: " + it.getName() + ":" + it.getValue())
  }
  
  return restClientResponse.getData()
  </code></pre>
  
  <p>Logs:</p>
  
  <pre><code>14 Apr 2015 17:25:04,561 uui.RestService java.io.StringReader@2a2e625
  14 Apr 2015 17:25:04,562 uui.RestService What is in StringReader
  14 Apr 2015 17:25:04,568 uui.RestService {"rbscResponse":{"errorMessage":"Unexpectederror","INSTALLATIONNUMBER":"","PRODUCTCOMMONNAME":"","errorCode":"30003","BTWSID":"","SERVICELINETYPE":"","CUSTOMERTYPE":"","RBSID":""}}
  14 Apr 2015 17:25:04,590 uui.RestService HEADER: Date:Tue, 14 Apr 2015 16:28:20 GMT
  14 Apr 2015 17:25:04,591 uui.RestService HEADER: Content-Type:application/json
  14 Apr 2015 17:25:04,591 uui.RestService HEADER: X-Powered-By:Servlet/2.5 JSP/2.1
  14 Apr 2015 17:25:04,591 uui.RestService HEADER: X-Cache:MISS from someproxy
  14 Apr 2015 17:25:04,592 uui.RestService HEADER: X-Cache-Lookup:MISS from someproxy:8080
  14 Apr 2015 17:25:04,592 uui.RestService HEADER: Via:1.0 someproxy (squid/3.1.10)
  14 Apr 2015 17:25:04,593 uui.RestService HEADER: Connection:close
  </code></pre>
  
  <p>Any ideas as to why the response body isn't being parsed into a <code>JsonSlurper</code> Object?</p>