Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

REST Assured Tutorial 20 – How to Send a JSON/XML File as Payload to Request

Posted on 03/21/2025 By admin

As a part of End to End REST Assured Tutorial , in this post We will learn about “How to send a JSON/XML file as payload to request in Rest Assured”.

Suppose you have a request payload (JSON or XML) in a file and you required to directly send that file as a payload to request in stead of reading it first and then passing. Sending file as a payload directly is a good idea when you have static payloads or minimal modification.

“body()” method of RequestSpecification interface is a overloaded method to allow you to pass payload in different ways. We will use body() method which accepts “File” as argument. Javadoc is below:-

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.

package DifferentWaysOfPassingPayloadToRequest;

import java.io.File;

import org.hamcrest.Matchers;
import org.testng.annotations.Test;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;

public class PassFileAsPayload {
        
        @Test
        public void passFileAsPayload()
        {
                // Creating a File instance 
                File jsonDataInFile = new File("src/test/resources/Payloads/AuthPayload.json");
                
                //GIVEN
                RestAssured
                    .given()
                                .baseUri("https://restful-booker.herokuapp.com/auth")
                                .contentType(ContentType.JSON)
                                .body(jsonDataInFile)
                // WHEN
                        .when()
                                .post()
                                // THEN
                        .then()
                                .assertThat()
                                .statusCode(200)
                                .body("token", Matchers.notNullValue())
                                .body("token.length()", Matchers.is(15))
                                .body("token", Matchers.matchesRegex("^[a-z0-9]+$"));
        }

}


Step 1:- Create a .xml 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.

@Test
        public void passXMLFileAsPayload()
        {
                // Creating a File instance 
                File jsonDataInFile = new File("src/test/resources/Payloads/AuthPayload.xml");
                
                //GIVEN
                RestAssured
                    .given()
                                .baseUri("https://restful-booker.herokuapp.com/auth")
                                // Since I am passing payload as xml. Anyway it is optional as Rest Assured automatically identifies.
                                .contentType(ContentType.XML)
                                .body(jsonDataInFile)
                // WHEN
                        .when()
                                .post()
                                // THEN
                        .then()
                                .assertThat()
                                .statusCode(200);
        }

You can clone/download example repo here.

If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading

#HappyLearning

Uncategorized

Post navigation

Previous Post: Test Automation Is Needed But Not As Magic
Next Post: REST Assured Tutorial 66 – How To Parse A JSON Array Response To A Java List In Rest Assured?

Related Posts

August 19, 2018 – Make Selenium Easy Uncategorized
April 22, 2018 – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized
identify angularjs Uncategorized
indepDep – Make Selenium Easy Uncategorized
October 13, 2018 – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark