About this lesson
REST Assured XML response validation: reading the raw XML body
Learn REST AssuredAdvancedXML Responses
How it works
Not every API speaks JSON. SOAP services, older enterprise systems and many feeds still return XML, and REST Assured handles it with the same request chain. testkru /xml/users answers with a Content-Type of application/xml with a utf-8 charset and a document that opens with an XML declaration, followed by a users root element holding one user element per person.
The simplest possible check is on the raw text. getBody().asString() (or the shorter asString() on the response, which returns the same text) gives you the document as a Java String, and contains() answers whether a fragment is present. It is a quick sanity test: did the call return XML, and does it mention the record you expect?
JSON tools do not work here. Reading this body through jsonPath() throws a JsonPathException because the text is not JSON. For structured reads, REST Assured offers xmlPath(), which walks the element tree with GPath expressions, and that is the next lesson.
About the assignment
Fetch the XML list of users, keep the body as plain text, and report whether it contains the XML declaration, the root users tag and the name John Doe.
What you will do
- Send a GET to
/xml/usersand keep theResponse. - Turn the body into a
StringwithgetBody().asString(). - Test the three fragments from the starter comment with
contains(). - Print each boolean after its label, starting with
Contains XML declaration:.
Common mistakes
Searching for the wrong case
contains()is case sensitive, so a lower-case name or an upper-case tag is reported as missing. Copy fragments exactly as they appear in the document.Expecting the root tag with attributes
Here the root element is written without attributes, so the plain opening tag is present. With other APIs it may carry a namespace, and a fragment check for the bare tag would fail. For anything beyond a smoke test, parse the XML.
Relying on string checks for real assertions
A name can appear inside a different element, or in a comment, and still make
contains()true. UsexmlPath()orthen().body()with an XML path when the exact location of a value matters.
In a real test suite
String checks on XML are best kept for smoke tests and for confirming content negotiation worked. Proper XML tests also assert the content type, then use XmlPath or Hamcrest XPath matchers, and sometimes validate the whole document against an XSD schema.
Related lessons
- Extract from XML with XmlPathReads values from the same XML with XmlPath.
- Content NegotiationAsks a single endpoint for XML or JSON through content negotiation.
- XML Product ValidationExtracts fields from the products XML endpoint.
To try one of these calls on any API endpoint without a task, open the Online Selenium Java Compiler (REST Assured is included).
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import static io.restassured.RestAssured.*;
public class Main {
public static void main(String[] args) {
RestAssured.baseURI = "https://testkru.com/api";
// TODO: GET /xml/users
// TODO: Get the body as a String
// TODO: Check if it contains "<?xml", "<users>", and "John Doe"
}
}