rockyPeoplesChamp
rockyPeoplesChamp

Reputation: 661

How to get properties from a xml response coming from a Rest call

I am using rest-client-builder:2.0.1 plugin in grails.

I am calling a rest url with a xml response and I am getting back a xml response also.

Here is my rest call,

def url = "http://ab-rest/getDetails"

def xmlBody = "<someGrp><id>CAP00001-1</id><Name>XX642105YP</Name>"
xmlBody = xmlBody+"<Grade>1</Grade><AccessCode></AccessCode>"
xmlBody = xmlBody+"<productCode>ABC</productCode>"
xmlBody = xmlBody+"<imageuri>www.abcd.com/232134</imageuri></imageuris><someGrp>"

log.debug "image processor xmlBody: ${xmlBody}"

def resp = rest.post(url) {
    header "Content-Type", "application/xml"
    header "X-Requested-With", "XMLHttpRequest"
    header "X-LTCallingApplicationName", "ABC"
    header "X-LTCallingUser", "TEST"
    header "X-LTCallingApplicationInstance", "System"
    header "X-LTCallingApplicationId", "70"
    xml xmlBody
}
resp.xml instanceof GPathResult

log.debug " resp.status "+resp.status
log.debug "image processor response xml: ${resp.xml}"

If I do resp.status here it returns 200 but response.xml just returns the property values in a concatenated form. Like below,

ID01-1CAP01-1http://qa.imagecache.cir.lifetouch.net/imagecache/service/imagecache/didimage/bd56a1783eb32b88a31964888cbba066d58961a4200jpg

But expected xml was,

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<images>
    <image>
        <id>ID01-1</id>
        <capturesession>CAP01-1</capturesession>
        <uri>http://ab-rest.net/imagecache/didimage/bd56a1783eb32b88a31964888cbba066d58961a4</uri>
        <status>200</status>
        <filetype>jpg</filetype>
    </image>
</images>

When I do a resp.text I get the below in as a string,

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><images><image><id>ID01-1</id><capturesession>CAP01-1</capturesession><uri>http://ab-rest.net/imagecache/didimage/bd56a1783eb32b88a31964888cbba066d58961a4</uri><status>200</status><filetype>jpg</filetype></image></images>

Now I want to get the properties from the response, like if I wantto get the uri how to do that?

Upvotes: 1

Views: 297

Answers (1)

moskiteau
moskiteau

Reputation: 1102

It return you a parsed xml, check the class and it will be a XmlSlurper.

e.g.

resp.xml.image.find {
  log.debug it.uri
}

Upvotes: 1

Related Questions