In this blog, we are going to discuss more about POST request testing and automation using RestAssured. Request Body parameter is also called payload. In our previous blog we saw how to send payload as strings.
There are three ways to send json payload.
- Using Json File
- Using Org.JSON
- Using POJO
In this tutorial we will learn about passing json data as a file.
Create a .json file in “src/test/resources” folder and write payload in that.
Mention this file location using the File class in Java. Your file object will look like this:
JSON, as you know, is nothing more than a data representation using Object and Array. As a result, we can also construct payloads using the JSONObject and JSONArray classes. To use this feature, however, you must include the org.json library in your project. To obtain the org.json library, include this dependency in your project.
So here's the code and explanation for converting above JSON to JSONObject.
Now we need to pass the json object in the body as shown below: