【问题标题】:How to delete specific parameter using their id in Rest Assured?如何在 Rest Assured 中使用其 id 删除特定参数?
【发布时间】:2018-09-24 13:25:04
【问题描述】:

下面是json响应

{
  "details": [
    {
      "UserName": "john",
      "id": "abc_123",
      "LastName": "smith"
    }
  ]
}

我只需要删除UserName 参数:

request.delete("http://localhost:8080/details/id/UserName");

上面的代码似乎不起作用,我的预期如下

{
  "details": [
    {
      "id": "abc_123",
      "LastName": "smith"
    }
  ]
}

【问题讨论】:

    标签: json rest-assured


    【解决方案1】:

    在 SO 上发布问题之前,请检查Minimal, Complete, and Verifiable example,有人可以提供帮助,但我们需要事先知道您尝试过什么。

    要回答您的问题,您应该使用 PUT 而不是 DELETE 因为您正在尝试更新有效负载。 DELETE 顾名思义就是删除整个资源

    查看link了解更多详情

    PUT 调用是特定于资源的,因此您必须提及应该影响哪个实体。

    我根据您提供的详细信息提出了一个示例代码

    在此处使用 HashMap,但您也可以发布正文 as such 或使用 POJOJSONObject

    {
    
        Map < String, Object > map = new HashMap < > ();
        map.put("details", Arrays.asList(new HashMap < String, Object > () {
            {
                put("id", "abc_123");
                put("LastName", "smith");
            }
        }));
    
        RequestSpecification req = RestAssured.given();
        req.header("Content-Type", "application/json");
        req.body(map).when();
        Response resp = req.put("http://localhost:8080/details/id/abc_123");
    
        String body = resp.asString();
        System.out.println("Response is : " + body);
    }
    

    【讨论】:

      猜你喜欢
      • 2018-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-23
      • 2017-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多