如何将数组值发送到Rest Assured请求的一个键。

huangapple go评论79阅读模式
英文:

How to send array value for one the key for rest assured request

问题

{
  "class": "A",
  "subUsecases": [
    "string1",
    "String2"
  ]
}

To generate the above, I am creating the Array object and sending the request but its turning out to be something like this in the rest assured request.

{
  "Class": "A",
  "subClass": "[\"String1\",\"String2\"]"
}

Due to the above actual result, the API is treating subClass as a string instead of an array. However, the code requires it to be an array for subClass.

I am using a HashMap to create the above, like this:

@Test
public void mm(){
    HashMap<String,String> queryParam = new HashMap<>();
    queryParam.put("class", "A");
    queryParam.put("subUsecases", arrayMethod("string1", "String2"));
}

public String arrayMethod(String s1, String s2){
    org.json.simple.JSONArray array = new JSONArray();

    array.add(s1);
    array.add(s2);

    return array.toJSONString();
}

queryParam is being sent as the JSON body.

Now, how can I send the expected JSON body instead of the actual JSON body? Thanks in advance.


<details>
<summary>英文:</summary>

Below is the expected request which should hit the system but while sending it the array becoming string? 

Expected
`{
&quot;class&quot;: &quot;A&quot;,
  &quot;subUsecases&quot;: [
    &quot;string1&quot;,
    &quot;String2&quot;
  ]
}`

To generate the above, I am creating the Array object and sending the request but its turning out to be some like this in rest assured request.

Actual

{"Class": "A", "subClass": "[&quot;String1&quot;,&quot;String2&quot;]"}


Due to above actuall result, api is thinking it a string for subClass and not treating it as array. But code wants it to be an array for subClass.

I am using hasmap to create the above. Like this

 

    @Test
        public void mm(){
            HashMap&lt;String,String&gt; queryParam = new HashMap&lt;&gt;();
            queryParam.put(&quot;CLASS&quot;,&quot;A&quot;);
            queryParam.put(&quot;subClass&quot;, arrayMethod(&quot;String1&quot;,&quot;String2&quot;));
        }
    
        public String arrayMethod(String s1, String s2){
            org.json.simple.JSONArray array = new JSONArray();
    
            array.add(s1);
            array.add(s2);
    
            return array.toJSONString();
        }

queryParam is going as jsonbody.

Now How to send as expected json body instead of actual json body.
Thanks in advance.

</details>


# 答案1
**得分**: 4

将`Map`更改为:

```java
HashMap<String, Object> queryParam = new HashMap<>();

并且创建一个普通的列表或数组,而不是使用JSONArray对象。array.toJSONString()已经生成了一个JSON字符串,你可以将其作为值放入另一个对象中。

queryParam.put("subClass", Arrays.asList("String1", "String2"));
英文:

Change Map to:

HashMap&lt;String, Object&gt; queryParam = new HashMap&lt;&gt;();

And create a regular list or array instead of using JSONArray object. array.toJSONString() produces already JSON string which you put as value to another object.

queryParam.put(&quot;subClass&quot;, Arrays.asList(&quot;String1&quot;, &quot;String2&quot;));

huangapple
  • 本文由 发表于 2020年8月27日 16:04:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63611695.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定