

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to handle static JSON in Rest Assured?
We can handle static JSON in Rest Assured. This can be done by storing the entire JSON request in an external file. First, the contents of the file should be converted to String.
Then we should read the file content and convert it to Byte data type. Once the entire data is converted to Byte, we should finally convert it to string. We shall utilize an external JSON file as a payload for executing a POST request.
Let us create a JSON file, say payLoad.json, and add a request body in the below JSON format. This is created within the project.
{ "title": "API Automation Testing", "body": "Rest Assured", "userId": "100" }
Example
Code Implementation
import org.testng.annotations.Test; import static io.restassured.RestAssured.*; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import io.restassured.RestAssured; public class NewTest { @Test void readJSONfile() throws IOException { //read data from local JSON file then store in byte array byte[] b = Files.readAllBytes(Paths.get("payLoad.json")); //convert byte array to string String bdy = new String(b); //base URL RestAssured.baseURI = "https://jsonplaceholder.typicode.com"; //input details with header and body given().header("Content-type", "application/json").body(bdy) //adding post method .when().post("/posts").then().log().all() //verify status code as 201 .assertThat().statusCode(201); } }
Output
- Related Questions & Answers
- Validate JSON Schema in Rest Assured.
- How to verify JSON response headers in Rest Assured?
- How to handle responses in text format in Rest Assured?
- What is JSON parsing in Rest Assured?
- How to get a JSON field in a nested JSON using Rest Assured?
- How to get a JSON array field in a nested JSON using Rest Assured?
- How to verify a JSON response body using Assertions in Rest Assured?
- How to get JSON fields(nodes) based on conditions using Rest Assured?
- How to extract the whole JSON response as a string in Rest Assured?
- How to iterate through and access the JSON array elements using Rest Assured?
- Explain how to get the size of a JSON array response in Rest Assured.
- What is Rest Assured?
- How to validate XML response in Rest Assured?
- How to get the size of an array within a nested JSON in Rest Assured?
- How to use Assertion in response in Rest Assured?
Advertisements