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


i'm using groovy's restclient.java post plain text server, response json i'm having issues figuring out why response being parsed stringreader object instead of jsonslurper object.

from understand response body should parsed object based on content type in response header, response header application/json seems it's defaulting , parsing response stringreader object. i'm not sure if i'm defining headers incorrectly or whether need specify own success handler?? have other methods use post method , work fine, posting xml , expecting xml in response think headers work out fine in instance.

below how i'm attempting , logs, can see response headers content-type application/json i'm baffled why restclientresponse.getdata() stringreader object instead of 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 in stringreader")  //printing out 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 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 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 someproxy 14 apr 2015 17:25:04,592 uui.restservice header: x-cache-lookup:miss 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 why response body isn't being parsed jsonslurper object?

the solution remove content-type argument restclient constructor , specify requestcontenttype in post arguments so:

def restclient = new restclient("http://this.doesnt.matter") ... def restclientresponse = restclient.post(     body:"requestpayload:accountprofile",     requestcontenttype: 'text/plain' ) 

the problem (lack of) understanding supplying restclient constructor default content-type does. assumed supplying restclient constructor content-type of text/plain set content-type text/plain in request header work out content-type in response header parser use response body. however, think providing restclient argument of text/plain when receives response ignores content-type in response headers , uses content-type supplied in restclient constructor.

thanks @dmahapatro pointing me towards this question lead me read post status update twitter! section of documentation


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -