Dynamically replacing values in XML and posting

1,073 views
Skip to first unread message

prvns

unread,
Jun 2, 2017, 2:14:02 PM6/2/17
to REST assured
I have a SOAP Service and it accepts the XML body below

<?xml version="1.0" encoding="UTF-8"?> <body> <userDetails> <firstName>John</firstName> <lastName>John</lastName> </userDetails> </body>

Now I want to replace the value in tag <firstName> and <lastName> dynamically while posting this XML Body.


Is there a way to do that in RestAssured?

Johan Haleby

unread,
Jun 3, 2017, 10:46:46 AM6/3/17
to rest-a...@googlegroups.com
Hmm what do you mean by dynamically? If you mean that you want to calculate firstName and lastName based on the content of the request (like headers etc) you can do that with a filter. Also rest assured allows you to create JSON and XML payloads from hashmaps so you can use that to construct the request.

/Johan

--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

prvns

unread,
Jun 3, 2017, 12:03:52 PM6/3/17
to REST assured
Hi Johan...

Ok I see what you are saying. I read about constructing payloads using hashmap from here https://github.com/rest-assured/rest-assured/wiki/Usage#create-json-from-a-hashmap

However let's say I have the XML in my original post. Can I replace the <firstName> and <lastName> values with the ones in the hashmap? for example

contextMap.put("firstName", "John");
contextMap.put("lastName", "Doe");

Now the values in the above hashmap should replace the one in the XML payload. Something like below

<body> <userDetails> <firstName>$firstName</firstName> <lastName>$lastName</lastName> </userDetails> </body>



To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.

Johan Haleby

unread,
Jun 4, 2017, 4:20:06 AM6/4/17
to rest-a...@googlegroups.com
Something like this:

String firstName = ...
String lastName = ...
Map<String, Object> xml = new HashMap<String, Object>() {{
put("body", new HashMap<String, Object>() {{
put("userDetails", new HashMap<String, Object>() {{
put("firstName", firstName);
put("lastName", lastName);
}});  
}});
}};

/Johan


To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured+unsubscribe@googlegroups.com.

prvns

unread,
Jun 4, 2017, 8:09:28 AM6/4/17
to REST assured
Ok. Thanks Johan. May be complex XML will need more complex data structure. I'm using Velocity Context right now to achieve what I was saying.

Johan Haleby

unread,
Jun 4, 2017, 1:19:06 PM6/4/17
to rest-a...@googlegroups.com
Yes using some form of templating engine is one option, another is to use JAXB.

/Johan
Sent from my phone

prvns

unread,
Jun 4, 2017, 1:25:45 PM6/4/17
to REST assured
Thanks a lot Johan!
Reply all
Reply to author
Forward
0 new messages