Emil
Emil

Reputation: 19

How to send post request, when i have params in array?

I have a post method in Postman which looks like this below:

{
    "name": "Produkt 1",
    "description": "Opis produktu",
    "price": 2500,
    "tax": 23.0,
    "currency": "PLN",
    "producer": "Marka",
    "quantity": 100,
    "quantity_type": "unit",
    "producer_code": "12345",
    "ean_code": "9638-5074",
    "is_active": 1,
    "category": "lorem-ipsum",
    "variant_group_id": "",
    "attributes": [
        {
            "attribute": "laudantium",
            "value": "1",
            "is_variant": "1"
        },
        {
            "attribute": "brand",
            "value": "Brand Y"
        }
    ]
}

Try to send this method by Rest Assured, but I get all the time response, that I need to send attributes (Status 422).

Here is my RestAssured code:

response =given()
                .log()
                .params()
                .request()
                .header("Accept","application/json")
                .header("Content-Type", "application/json")
                .queryParam("name",args[0])
                .queryParam("description",args[1])
                .queryParam("price",args[2])
                .queryParam("tax",args[3])
                .queryParam("currency",args[4])
                .queryParam("producer",args[5])
                .queryParam("quantity",args[6])
                .queryParam("quantity_type",args[7])
                .queryParam("producer_code",args[8])
                .queryParam("ean_code",args[9])
                .queryParam("category",args[10])
                .queryParam("variant_group_id",args[11])
                .queryParam("is_active",args[12])
                .queryParams("attributes.attribute[0]","laudantium")
                .queryParam("attributes.value[0]","1")
                .queryParam(".is_variant[0]","1")
                .post(HOST+"/products");

How to send valid params of attributes? I quess that there is a problem with queryParam notation..

Upvotes: 0

Views: 2094

Answers (1)

Emil
Emil

Reputation: 19

I figured out this thanks to you guys. I had to change my method body on like this:

    JSONObject childJSON = new JSONObject();
    childJSON.put("attribute", "sit-omnis");
    childJSON.put("value",value);
    childJSON.put("is_variant",is_variant);

    JSONArray array = new JSONArray();
    array.add(childJSON);

    JSONObject requestParams = new JSONObject();
    requestParams.put("name",args[0]);
    requestParams.put("description",args[1]);
    requestParams.put("price",args[2]);
    requestParams.put("tax",args[3]);
    requestParams.put("currency","PLN");
    requestParams.put("producer",args[4]);
    requestParams.put("quantity",args[5]);
    requestParams.put("quantity_type","unit");
    requestParams.put("producer_code",args[6]);
    requestParams.put("ean_code","9638-5074");
    requestParams.put("is_active",isActive);
    requestParams.put("category",args[7]);
    requestParams.put("variant_group_id",args[8]);
    requestParams.put("attributes",array);

    response =given()
            .log()
            .params()
            .request()
            .header("Accept","application/json")
            .header("Content-Type", "application/json")
            .body(requestParams.toJSONString())
            .post(INEGRATOR_HOST+"/products");
    checkStatusAndShowBody(codeStatus);

First, I create all needed params as a JSON, than I converted 3 params (on top) to JSON Array, because param "attributes" was an Array. Than I simply put variable array, as a value of "attributes" and it works.

My methods need to be authenticated by HMAC-512 but I missed it in this issue cos I think it wasnt so importnant. Anyway, I got code 200 and good response. Thx guys!

Upvotes: 2

Related Questions