An API may accept a JSON Array payload as well. For example:- Booking for multiple passengers at once. In this case, we may need to pass multiple JSON objects within a JSON array. An example is below:-
[
{
"firstname": "Amod",
"additionalneeds": "Breakfast",
"bookingdates": {
"checkin": "2021-08-01",
"checkout": "2021-08-02"
},
"totalprice": 222,
"depositpaid": true,
"lastname": "Mahajan"
},
{
"firstname": "Animesh",
"additionalneeds": "Breakfast",
"bookingdates": {
"checkin": "2021-07-01",
"checkout": "2021-07-01"
},
"totalprice": 111,
"depositpaid": true,
"lastname": "Prashant"
}
]
I just twisted Restful Booking API for multiple bookings at once. We need to add as many JSON Objects containing guest details as required in a JSON Array. For example:- I want to do two bookings at once so added two booking details.
So how can we create such payload?
We already know how to create a JSON Object using Map. A JSON Array in Java can be created using List or Set. So we need to perform below steps to create payload as above:-
- Create a JSON Object and add the first guest details.
- Create another JSON Object and add second guest details
- Create a List or Set object.
- Add both JSON Object to List.
// JSON Object for first guest
Map<String,Object> bookingOne = new HashMap<String,Object>();
bookingOne.put("firstname", "Amod");
bookingOne.put("lastname", "Mahajan");
bookingOne.put("totalprice", 222);
bookingOne.put("depositpaid", true);
Map<String,String> bookingDatesMapForAmod = new HashMap<>();
bookingDatesMapForAmod.put("checkin", "2021-08-01");
bookingDatesMapForAmod.put("checkout", "2021-08-02");
bookingOne.put("bookingdates", bookingDatesMapForAmod);
bookingOne.put("additionalneeds", "Breakfast");
// JSON Object for second guest
Map<String,Object> bookingTwo = new HashMap<String,Object>();
bookingTwo.put("firstname", "Animesh");
bookingTwo.put("lastname", "Prashant");
bookingTwo.put("totalprice", 111);
bookingTwo.put("depositpaid", true);
Map<String,String> bookingDatesMapForAnimesh = new HashMap<>();
bookingDatesMapForAnimesh.put("checkin", "2021-07-01");
bookingDatesMapForAnimesh.put("checkout", "2021-07-01");
bookingTwo.put("bookingdates", bookingDatesMapForAnimesh);
bookingTwo.put("additionalneeds", "Breakfast");
// Creating JSON array to add both JSON objects
List<Map<String,Object>> jsonArrayPayload = new ArrayList<>();
jsonArrayPayload.add(bookingOne);
jsonArrayPayload.add(bookingTwo);
As Booking API does not accept JSON Array payload, but to show as an example, I am just passing it. Because of invalid payload, it is giving internal server error. You can ignore that and focus on creating a JSON Array.
Example:-
package DifferentWaysOfPassingPayloadToRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
public class CreatingNestedJsonArray {
@Test
public void CreatingNestedJsonObjectTest()
{
// JSON Object for first guest
Map<String,Object> bookingOne = new HashMap<String,Object>();
bookingOne.put("firstname", "Amod");
bookingOne.put("lastname", "Mahajan");
bookingOne.put("totalprice", 222);
bookingOne.put("depositpaid", true);
Map<String,String> bookingDatesMapForAmod = new HashMap<>();
bookingDatesMapForAmod.put("checkin", "2021-08-01");
bookingDatesMapForAmod.put("checkout", "2021-08-02");
bookingOne.put("bookingdates", bookingDatesMapForAmod);
bookingOne.put("additionalneeds", "Breakfast");
// JSON Object for second guest
Map<String,Object> bookingTwo = new HashMap<String,Object>();
bookingTwo.put("firstname", "Animesh");
bookingTwo.put("lastname", "Prashant");
bookingTwo.put("totalprice", 111);
bookingTwo.put("depositpaid", true);
Map<String,String> bookingDatesMapForAnimesh = new HashMap<>();
bookingDatesMapForAnimesh.put("checkin", "2021-07-01");
bookingDatesMapForAnimesh.put("checkout", "2021-07-01");
bookingTwo.put("bookingdates", bookingDatesMapForAnimesh);
bookingTwo.put("additionalneeds", "Breakfast");
// Creating JSON array to add both JSON objects
List<Map<String,Object>> jsonArrayPayload = new ArrayList<>();
jsonArrayPayload.add(bookingOne);
jsonArrayPayload.add(bookingTwo);
//GIVEN
RestAssured
.given()
.baseUri("https://restful-booker.herokuapp.com/booking")
.contentType(ContentType.JSON)
.body(jsonArrayPayload)
.log()
.all()
// WHEN
.when()
.post()
// THEN
.then()
.assertThat()
// Asserting status code as 500 as it does not accept json array payload
.statusCode(500)
.log()
.all();
}
}
You can see passing JSON Array body in the output below:-
[RemoteTestNG] detected TestNG version 7.0.0
Request method: POST
Request URI: https://restful-booker.herokuapp.com/booking
Proxy: <none>
Request params: <none>
Query params: <none>
Form params: <none>
Path params: <none>
Headers: Accept=*/*
Content-Type=application/json; charset=UTF-8
Cookies: <none>
Multiparts: <none>
Body:
[
{
"firstname": "Amod",
"additionalneeds": "Breakfast",
"bookingdates": {
"checkin": "2021-08-01",
"checkout": "2021-08-02"
},
"totalprice": 222,
"depositpaid": true,
"lastname": "Mahajan"
},
{
"firstname": "Animesh",
"additionalneeds": "Breakfast",
"bookingdates": {
"checkin": "2021-07-01",
"checkout": "2021-07-01"
},
"totalprice": 111,
"depositpaid": true,
"lastname": "Prashant"
}
]
HTTP/1.1 500 Internal Server Error
Server: Cowboy
Connection: keep-alive
X-Powered-By: Express
Content-Type: text/plain; charset=utf-8
Content-Length: 21
Etag: W/"15-/6VXivhc2MKdLfIkLcUE47K6aH0"
Date: Thu, 27 Feb 2020 17:21:58 GMT
Via: 1.1 vegur
Internal Server Error
PASSED: CreatingNestedJsonObjectTest
===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================