【问题标题】:How to use the parameter from POST body for GET route如何将 POST 正文中的参数用于 GET 路由
【发布时间】:2014-04-16 20:53:34
【问题描述】:

嘿,我是新手,请放心,我设法让 POST 路由测试用例工作,下面的测试用例的响应正文是

public void submissionsUserPostTest() {

    JSONObject data = new JSONObject();
    data.put("username", "XYZ");

    String jsonString = data.toJSONString();

    Response request = given().contentType("application/json")
            .body(jsonString).expect().statusCode(201).when()
            .post(postRoute);

    request.print();
}

身体

{"createdAt":"2014-04-16T20:04:40.560Z","updatedAt":"2014-04-16T20:04:40.560Z","id":"51de8ae0-","_links":{"self":{"href":"/assignments/51de8ae0-c43e-44c3-b46d-f48a25739385"}},"username":"XYZ","uploadDate":"2014-04-16T20:04:40.560Z"}

所以我的问题是 如何使用 POST 路由生成的 id 到我的 GET 路由 /assignments/{id} 使用请求??

【问题讨论】:

  • 我不明白。 postRoute 是什么?
  • postRoute 是一个包含“someaddress/assignments”的字符串
  • 你想检索什么? JSON 中的值?
  • 是的,我应该提到它
  • 您可以编辑您的问题以添加更多详细信息。您是否研究过任何 JSON 解析器?

标签: java rest-assured


【解决方案1】:

你的意思是自我链接吗?在这种情况下,您可以这样做:

String assignment = 
given().
        contentType("application/json").
        body(jsonString)
when().
        post(postRoute).
then().
        statusCode(201).
extract().
      path("_links.self.href");

when().
       get(assignment).
then().
       ...

【讨论】:

    【解决方案2】:

    使用类似的东西

    String id = response.jsonPath().getString("id");
    

    然后传递存储的值 [i.e. id] 到您的 GET 请求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-29
      • 1970-01-01
      • 2018-04-05
      • 2022-08-09
      • 2015-02-26
      • 1970-01-01
      相关资源
      最近更新 更多