How to Send a JSON/XML File as Payload to Request using Rest Assured

REST Assured is a powerful Java library used for testing RESTful APIs. It simplifies HTTP request interactions, allowing you to handle HTTP responses in a structured way. Often, when testing APIs, you may need to send a payload in JSON or XML format as part of the request body. In this blog post, we’ll look at how to send JSON and XML payloads to REST APIs using Rest Assured.


Prerequisites

Before getting started, make sure you have the following:

  • Java Development Kit (JDK) installed.
  • Maven as your build tool (optional, but highly recommended).
  • Rest Assured dependency added to your Maven pom.xml file:
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>5.3.0</version>
</dependency>

Sending a JSON File as Payload

JSON (JavaScript Object Notation) is commonly used in RESTful APIs due to its lightweight and easy-to-read format. To send JSON data as a payload, you can create a JSON file and pass it to the request body.

Step 1: Create a JSON File

Create a JSON file (e.g., data.json) with sample data. This file could represent any valid JSON structure required by the API endpoint.

Example data.json:

{

    "name": "John Doe",

    "email": "john.doe@example.com",

    "age": 30

}

Step 2: Read JSON File and Send as Payload

Using Rest Assured, you can read the JSON file and send it as a request payload. Here’s how you can do it:

import io.restassured.RestAssured;

import io.restassured.response.Response;


import java.io.File;


public class RestAssuredTest {

    public static void main(String[] args) {

        // Specify the base URI for the API endpoint

        RestAssured.baseURI = "https://api.example.com";

        

        // Read the JSON file as a File object

        File jsonFile = new File("src/test/resources/data.json");

        

        // Send POST request with JSON payload

        Response response = RestAssured

            .given()

            .header("Content-Type", "application/json")

            .body(jsonFile) // Use file as body

            .post("/createUser"); // endpoint


        // Print the response for verification

        System.out.println("Response: " + response.asPrettyString());

    }

}

Here:

  • We specify the base URI and endpoint where we want to send the request.
  • We read the JSON file as a File object and set it as the request body.
  • The header("Content-Type", "application/json") sets the request header to application/json.
  • Finally, we send a POST request and print the response.

Step 3: Verify the Response

You can add assertions to verify if the response matches expected values. For example:

response.then().statusCode(201); // Check for successful creation

Sending an XML File as Payload

Similar to JSON, XML (Extensible Markup Language) is another popular format for API payloads, particularly in legacy systems.

Step 1: Create an XML File

Create an XML file (e.g., data.xml) with sample data. Ensure the XML structure adheres to the API’s specifications.

Example data.xml:

<user>

    <name>John Doe</name>

    <email>john.doe@example.com</email>

    <age>30</age>

</user>

Step 2: Read XML File and Send as Payload

Using Rest Assured, you can read the XML file and send it as a request payload. Here’s the sample code:

import io.restassured.RestAssured;

import io.restassured.response.Response;


import java.io.File;


public class RestAssuredXMLTest {

    public static void main(String[] args) {

        // Specify the base URI for the API endpoint

        RestAssured.baseURI = "https://api.example.com";

        

        // Read the XML file as a File object

        File xmlFile = new File("src/test/resources/data.xml");

        

        // Send POST request with XML payload

        Response response = RestAssured

            .given()

            .header("Content-Type", "application/xml")

            .body(xmlFile) // Use file as body

            .post("/createUser"); // endpoint


        // Print the response for verification

        System.out.println("Response: " + response.asPrettyString());

    }

}

Here:

  • The Content-Type header is set to application/xml.
  • We use the XML file as the request body.
  • The code sends the request and prints the response, similar to the JSON example.

Step 3: Verify the Response

Like the JSON example, you can add assertions to validate the XML response:

response.then().statusCode(201); // Check if the user was created successfully

Followers