Processing encoded response using HTTPBuilder / Groovy

I am new to groovy and started using it to test some REST services. I had a problem parsing my XML response from our service because Content is not allowed in the prolog. After some searching, I came across a message saying that in the beginning there might be a byte order marker. To compensate, I followed their approach to crop characters before the first <and then parse the answer. Although this works, I was also told that the problem is that the response is returned as "Transfer-Encoding: chunked".

Using HTTPBuilder, is there a way to handle response replies without trimming characters? If I try:

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

I get the message "Invalid content in prolog message." However:

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

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

+3
source share
3 answers

I had the same problem when I needed to interact with the IIS server. The returned XML had a false character before the actual XML returned by the web server. I worked around this as follows:

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

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

Maybe something like:

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

Hope this helps.

0
source

I had a problem uninstalling the IIS server through https. Here is a small addition to Wim Deblaue's answer to the POST request. You must send a different type of request than you expect in return.

Send a POST with XML as the request type and TEXT as the response type. Then parse the text response in XML. It 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));
0
source

Source: https://habr.com/ru/post/1775531/


All Articles