groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ste Wootten <stewoot...@gmail.com>
Subject Grails RestClient accessing POST response XML
Date Tue, 31 Mar 2015 09:30:35 GMT
I'm using Groovy's RESTClient() to perform a POST with some XML as the
payload to a URL. The response I expect back is

<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope
    xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
        <ns2:getNetworkSessionsForUsernameResponse
            xmlns:ns2="http://webservice.foo.bar.net/">
            <return>
                <ip>1.1.1.1</ip>
                <platform>112</platform>
                <pop>ABC</pop>
                <site>LIP</site>
                <state>STATUS</state>
                <userId>ID1234</userId>
            </return>
        </ns2:getNetworkSessionsForUsernameResponse>
    </S:Body>
</S:Envelope>

I've curled to the URL and know that the response is correct, so it's not
an issue with the data that comes back. My issue is the format in which
RESTClient().post().getData() returns. If I do

def restClient = new RESTClient("www.someRestService.com", 'text/xml')

String soapRequest = """XML Payload..."""

def restClientResponse = restClient.post(body: soapRequest).getData()

log.info("RESPONSE: " + restClientResponse)
log.info("BODY: " +
restClientResponse.Body.getNetworkSessionsForUsernameResponse.return.userId)

log.info("BODY T: " +
restClientResponse.Body.getNetworkSessionsForUsernameResponse.return.userId.text())

log.info("NETWORK: " +
restClientResponse.getNetworkSessionsForUsernameResponse.return.userId)
log.info("NETWORK T: " +
restClientResponse.getNetworkSessionsForUsernameResponse.return.userId.text())

log.info("RETURN: " + restClientResponse.return.userId)
log.info("RETURN T: " + restClientResponse.return.userId.text())
log.info("USERID: " + restClientResponse.userId)
log.info("USERID T: " + restClientResponse.userId.text())
log.info("CHILDREN SIZE: " + restClientResponse.children().size())
log.info("CLASS: " + restClientResponse.getClass().getName())

What is logged is:

RESPONSE: 1.1.1.1112ABCLIPSTATUSID1234
BODY:
BODY T:
NETWORK:
NETWORK T:
RETURN:
RETURN T:
USERID:
USERID T:
CHILDREN SIZE: 1
CLASS: groovy.util.slurpersupport.NodeChild

So getData() seems to just be returning all the text nodes instead of a
parsed XML Object, which I'm sure I read somewhere that getData() parses
XML using XMLSlurper(). I read this
<http://marc.info/?l=groovy-user&m=127231413219292>article among many
others which seems exactly like my issue, but the answer given is exactly
what I'm doing isn't it? Am I accessing restClientResponse in the totally
wrong way?

Really appreciate any help!

Mime
View raw message