简体   繁体   English

当我在数组中有参数时,如何发送发布请求?

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

I have a post method in Postman which looks like this below: 我在Postman中有一个post方法,如下所示:

{
    "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). 尝试通过Rest Assured发送此方法,但是我得到了所有需要发送属性的时间响应(状态422)。

Here is my RestAssured code: 这是我的RestAssured代码:

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.. 我问queryParam表示法有问题。

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. 首先,我将所有需要的参数创建为JSON,然后将3个参数(在顶部)转换为JSON数组,因为参数“属性”是一个数组。 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. 我的方法需要通过HMAC-512进行身份验证,但是我在本期中错过了它,因为我认为它并不那么重要。 Anyway, I got code 200 and good response. 无论如何,我得到了代码200和良好的响应。 Thx guys! 谢谢你们!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM