zoran119
zoran119

Reputation: 11307

Consuming web service

I'm calling a web service like this (using rest plugin):

withRest(uri: "http://server.com") {
    def response = post(path: '/webservice', query: [q: 'test'])
    // process response
}

and getting the following response:

<?xml version="1.0" encoding="UTF-8" ?>
<RESPONSE>
    <MULTIPLE>
        <SINGLE>
            <KEY name="id">
                <VALUE>1</VALUE>
            </KEY>
            <KEY name="courseid">
                <VALUE>1</VALUE>
            </KEY>
            <KEY name="name">
                <VALUE>test</VALUE>
            </KEY>
            <KEY name="description">
                <VALUE>Test</VALUE>
            </KEY>
            <KEY name="descriptionformat">
                <VALUE>1</VALUE>
            </KEY>
            <KEY name="enrolmentkey">
                <VALUE>TEST</VALUE>
            </KEY>
        </SINGLE>
    </MULTIPLE>
</RESPONSE>

I'd like to convert this response to an object so that I can do something like this:

assert responseMap.id == 1

The only way I know how to do this is use Gpath expressions in the // process request block above to grab individual entries and build my responseMap. Is there any easier way? Does this response follow some 'standard' and are there functions to make my job easier?

Upvotes: 0

Views: 99

Answers (1)

tim_yates
tim_yates

Reputation: 171084

You can just do:

new XmlSlurper().parseText( response )
                .MULTIPLE.SINGLE.KEY
                .find { it.@name == 'id' }
                .VALUE.text() == '1'

To check the VALUE of the KEY tag with name="id" is 1

Upvotes: 4

Related Questions