SteWoo
SteWoo

Reputation: 468

Groovy RestClient not using correct response handler for Content-Type: application/json?

I'm using Groovy's RestClient.java 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 StringReader object instead of a JsonSlurper object.

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 application/json but it seems like the it's defaulting to and parsing the response into a StringReader 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.

Below is how I'm attempting it and my logs, as you can see the response headers Content-Type is application/json so I'm baffled as to why restClientResponse.getData() is a StringReader object instead of a JsonSlurper object

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()

Logs:

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

Any ideas as to why the response body isn't being parsed into a JsonSlurper Object?

Upvotes: 0

Views: 7205

Answers (1)

SteWoo
SteWoo

Reputation: 468

The solution was to remove the Content-Type argument from the RESTClient constructor and specify the requestContentType in the post arguments like so:

def restClient = new RESTClient("http://this.doesnt.matter")
...
def restClientResponse = restClient.post(
    body:"requestPayload:ACCOUNTPROFILE",
    requestContentType: 'text/plain'
)

The problem was my (lack of) understanding as to what supplying the RESTClient constructor with a default Content-Type does. I assumed by supplying the RESTClient constructor with the Content-Type of text/plain it would set the Content-Type to text/plain in the request header then work out from the Content-Type in the response header which parser to use for the response body. However, I think by providing the RESTClient with an argument of text/plain that when it receives a response it ignores the Content-Type in the response headers and uses the Content-Type that was supplied in the RESTClient constructor.

Thanks to @dmahapatro for pointing me towards this question which lead me to read the POST a status update to Twitter! section of the documentation

Upvotes: 2

Related Questions