I tried to make a POST request using POJO but getting "vHTTP/1.1 500 Internal Server Error", could you please advise why? When I pass body as string it works fine, but the passing the bookingDetails will throw the error. I think something is not right with the BookingDates class but I'm not sure why.
package bookings.POST;
import bookings.BookingBaseTest;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.junit.jupiter.api.Test;
public class CreateBooking extends BookingBaseTest {
@Test
public void createBooking(){
//Set json Data
BookingDetails bookingDetails = new BookingDetails();
bookingDetails.setFirstname("Jim");
bookingDetails.setLastname("Brown");
bookingDetails.setTotalprice(111);
bookingDetails.setDepositpaid(true);
BookingDetails.BookingDates bDates = new BookingDetails.BookingDates();
bDates.setCheckin("2018-01-01");
bDates.setCheckout("2019-01-01");
bookingDetails.setBookingDates(bDates);
bookingDetails.setAdditionalneeds("Breakfast");
Response response = RestAssured.given().
spec(requestSpec)
.body(bookingDetails)
.when()
.post();
response.then().log().all();
}
}
package bookings.POST;
/*
{
"firstname" : "Jim",
"lastname" : "Brown",
"totalprice" : 111,
"depositpaid" : true,
"bookingdates" : {
"checkin" : "2018-01-01",
"checkout" : "2019-01-01"
},
"additionalneeds" : "Breakfast"
}
*/
public class BookingDetails {
private String firstname;
private String lastname;
private int totalprice;
private boolean depositpaid;
private BookingDates bookingDates;
private String additionalneeds;
//Omit other getters and setters
public BookingDates getBookingDates() {
return bookingDates;
}
public void setBookingDates(BookingDates bookingDates) {
this.bookingDates = bookingDates;
}
static class BookingDates {
private String checkin;
private String checkout;
public String getCheckin() {
return checkin;
}
public void setCheckin(String checkin) {
this.checkin = checkin;
}
public String getCheckout() {
return checkout;
}
public void setCheckout(String checkout) {
this.checkout = checkout;
}
}
}
You should pass the json serialized body of the BookingDetails object in your request body. You can use ObjectMapper for serializing the object:
//serialize the bookingDetails object
ObjectMapper mapper = new ObjectMapper();
String bookingDetailsJson = mapper.writeValueAsString(bookingDetails);
And then pass it in your request body, also mentioning the contentType as JSON:
Response response = RestAssured.given().spec(requestSpec).contentType(JSON).body(bookingDetailsJson).when().post();