Hire QA – Specialized in QA Recruitment, Technical Interviews and Testing Solutions

How To Send A JSON/XML File As Payload To Request

Category: 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.

                 // Creating a File instance 
		File jsonFile = new File("D:/RestAssured/jsonPayload.json");
		
		//GIVEN
		RestAssured
		    .given()
				.baseUri("https://hireqa.co.in")
				.contentType(ContentType.JSON)
				.body(jsonFile)
		// 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]+$"));

Leave a Reply

Your email address will not be published. Required fields are marked *