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.
Pass JSON file as payload
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:
File jsonData = new File(“src\test\resources\jsondemo.json”)
Below is the post request:
Below is the output after code execution:
Creating Payload as JSON Object:
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:
Top comments (1)
Thanks for the post, as I'm new learner I am following your posts for learn Rest API Automation using RestAssured.