user7532276
user7532276

Reputation:

Array of object as request body in restassured

I usually able to create class as input for .body() and rest-assured read all the data correctly, but not with array.
I tried declaring the object class as an array, but rest-assured didn't accept it correctly as I want.
Can I send array of object as .body when using rest-assured?

Request Body

[
    {
        "product_type" : "1",
        "request_by" : "android",
    },
    {
        "product_type" : "2",
        "request_by" : "ios",
    }
]

The class I make

public class ProdReq {
    private String product_type;
    private String request_by;

    public String getProduct_type() {
        return product_type;
    }

    public void setProduct_type(String product_type) {
        this.product_type = product_type;
    }

    public String getRequest_by() {
        return request_by;
    }

    public void setRequest_by(String request_by) {
        this.request_by = request_by;
    }

The code I use to get response

ProdReq[] prodReq = new ProdReq[2]
//set the data
......
given().when().body(prodReq).post({{api_url}}).then().extract().response();

Should I make a JSONObject of the class (if possible), then put them in a JSONArray?

Upvotes: 2

Views: 4886

Answers (1)

baadnews
baadnews

Reputation: 128

@GFB Did you set up the ContentType? Try to use something like this:

List<ProdReq> prodReq = new ArrayList<>();
... set up the data. 

given().contentType(ContentType.JSON).when().body(prodReq).post({{api_url}}).then().extract().response();

I'm using serialization of the object to JSON body without any problems in my project.

Upvotes: 1

Related Questions