Question

I'm new to using groovy and have started to use it to test some REST services. I'm having an issue parsing my XML response from our service due to 'Content not allowed in prolog.' After awhile searching I came across a post saying there might be a Byte Order Marker at the beginning. To compensate I followed their approach to trim the characters before the first < and then parse the response. While this works, I was also told the issue is that the response is coming back as 'Transfer-Encoding: chunked'.

Using HTTPBuilder, is there a way to handle chunked responses without trimming characters off? If I try:

def http = new HTTPBuilder('url')
http.request( Method.valueOf("GET"), XML )

I get the 'Content not allowed in prolog message. However:

http.request( Method.valueOf("GET"), TEXT )

Works, but requires trimming the text until the first < before sending the response to XmlParser.

Was it helpful?

Solution

I had the same issue when I needed to interact with an IIS server. The XML returned had a bogus character in front of the actual XML returned by the web server. I worked around it like this:

StringReader reader = builder.get( path: 'rcserver/systeminfo.xml', contentType: ContentType.TEXT )
def text = reader.getText()
def xml = new XmlSlurper().parseText(text.substring(1));

OTHER TIPS

The HTTPBuilder class has a setContentEncoding() method that allows you to specify the response's content-type.

Maybe something like:

http.contentEncoding = ContentEncoding.Type.GZIP
http.request( Method.GET, XML)

Hope this helps.

I was having this problem as well hitting an IIS server over https. Here is a little addition to Wim Deblauwe's answer for a POST request. You have to send a different type in the request than you expect in the response.

Send a POST with XML as the request type and TEXT as the response type. Then, parse the text response into XML. This worked for me.

In Groovy:

def reader = http.request(Method.POST, ContentType.TEXT){
    uri.path = "myPath.api"
    send ContentType.XML, postBodyXml
}
def text = reader.getText()
def resultxml = new XmlSlurper().parseText(text.substring(1));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top