How To Send A JSON/XML File As Payload To Request using Rest Assured


In this tutorial, I will explain to pass a JSON or XML file as a payload to the request. This is needed when the payload is static or there is minimal change in the request payload. This can be done by using the body() method which accepts “File” as an argument. This is elaborated in Javadoc.

RequestSpecification body(File body)

This specify file content that’ll be sent with the request. This only works for the POST, PATCH and PUT http method. Trying to do this for the other http methods will cause an exception to be thrown.

Pass JSON file as payload

Step 1 – Create a .json file and write payload in that. Keep the file in “src/test/resources” folder.

Step 2 – Create a File in Java using “File” and pass to body() method.

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;


import org.junit.Test;

import io.restassured.http.ContentType;
import io.restassured.response.ValidatableResponse;

public class passJsonAsPayload {

	ValidatableResponse validatableResponse;

	public void createUser() {

		// Creating a File instance
		File jsonData = new File("src/test/resources/Payloads/jsondemo.json");

		// GIVEN

		// WHEN

		// THEN
               .body("", equalTo("Json_Test"))
			   .body("message", equalTo("Successfully! Record has been added."))



Similarly, we can pass an XML as a payload to request. The file passed within body() method should be of type .xml.

Congrates. You have learnt how to pass a JSON as a payload to the request. Happy Learning !!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s