Sending XML Request with REST-assured

Learn to send requests with XML request body and validate the response using REST-assured APIs.

1. By Parsing Custom Object to XML

1.1. Parsing Java Object to XML

If we can take the help of libraries such as Jackson or JAXB, for converting a custom object into XML String, it should be the preferred way. It helps in making the code more readable and manageable.

For example, if we have an API that takes the following request:

HTTP POST /users

<user>
	<name>lokesh</name>
	<email>admin@howtodoinjava.com</email>
	<gender>male</gender>
	<status>active</status>
</user>

We can create a Java object UserObject and annotate it with Jackson annotations so that it parses to the desired XML string.

@JacksonXmlRootElement(localName = "user")
class UserObject {

  @JsonInclude(JsonInclude.Include.NON_NULL)
  private Integer id;

  private String name;
  private String email;
  private String gender;
  private String status;
}

Now, we can use Jackson’s XmlMapper.writeValueAsString() method to get the XML string for the UserDTO instance. This will generate the XML as given previously.

UserObject newUser = new UserObject();
newUser.setName("lokesh");
newUser.setEmail("admin@howtodoinjava.com");
newUser.setGender("male");
newUser.setStatus("active");

String newUserXml = new XmlMapper().writeValueAsString(newUser);

1.2. Sending Request with REST-assured

Sending the XML request body is very simple. We need to set the content type to “application/xml” (or any other custom mediatype, if any) and then pass the XML string to the given().body(xml) method.

A complete example of sending the XML request to a POST API is:

@Test
public void createUserWithJSONObject_thenSuccess() 
  	throws JSONException, JsonProcessingException {

  UserObject newUser = new UserObject();
  newUser.setName("lokesh");
  newUser.setEmail("admin@howtodoinjava.com");
  newUser.setGender("male");
  newUser.setStatus("active");

  String newUserXml = new XmlMapper().writeValueAsString(newUser);

  given()
    .body(newUserXml)
    .contentType("application/xml")
    .queryParam("access-token", "xxxx")
  .when()
  	.post("/users")
  .then()
    .statusCode(201)
    .body("id", notNullValue())
    .body("name", equalTo("lokesh"))
    .body("gender", equalTo("male"))
    .body("status", equalTo("active"))
    .body("email", equalTo("admin@howtodoinjava.com"))
    .log().all();
}

2. By Reading XML from File

In automation testing, we may not have created DTO objects, and we may need to rely on hardcoded XML strings. In this case, creating such XML strings in separate XML files that are API specific is always better.

We read the XML file for each API and post its content to the API. In this approach, only obtaining the XML string is different and invoking the API is the same as the previous example.

File newUserXmlFile = new File("src/test/resources/requests/newUser.xml");

given()
  .body(newUserXmlFile)
  ...
.when()
	.post("/users")
.then()
  ...;

3. Conclusion

This short tutorial taught us to pass the XML requests to REST APIs using REST-assured. We learned to get the XML request strings from either custom Java objects or XML files in the filesystem. Then we learned to pass the XML to the API and validate the response.

Happy Learning !!

Sourcecode on Github

Comments

Subscribe
Notify of
guest
2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode