DEV Community

TaheraFirdose
TaheraFirdose

Posted on

Multiple ways to send Payload - Part 6

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.

  1. Using Json File
  2. Using Org.JSON
  3. 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.

image

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:

image

Below is the output after code execution:

image

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.

image

image

So here's the code and explanation for converting above JSON to JSONObject.

image

Now we need to pass the json object in the body as shown below:

image

Discussion (1)

Collapse
twitvital profile image
Vital

Thanks for the post, as I'm new learner I am following your posts for learn Rest API Automation using RestAssured.