Skip to content
CodeKruPro

Get and Validate XML Response

48/62

Get and Validate XML Response

The /xml/users endpoint returns data in XML format:

<users>
  <user><id>1</id><name>John Doe</name>...</user>
  ...
</users>

Task: Fetch the XML response and verify it contains XML data.

Expected output:

Contains XML declaration: true
Contains users tag: true
Contains John Doe: true

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

  1. Send a GET to /xml/users and keep the Response.
  2. Turn the body into a String with getBody().asString().
  3. Test the three fragments from the starter comment with contains().
  4. 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. Use xmlPath() or then().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.

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.

Main.java
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"
    }
}