简体   繁体   English

如何验证响应并从 Rest Assured 中的响应正文中提取值?

[英]How to validate a response and extract a value from Response body in Rest Assured?

I know how to extract a value from Response body and also how to validate a field from response body, but what the problem is我知道如何从响应体中提取值以及如何验证响应体中的字段,但问题是什么

void postRequest() {
    Response id = given()
            .auth()
            .preemptive()
            .basic(username,password)
            .contentType(ContentType.JSON)
            .body(----- some JSON body ------)
            .when()
            .post(baseurl+"postRequest")
            .then()
            .contentType(ContentType.JSON)
            .extract()
            .path("id")
            ;
}

with this, I can extract the value that I want to and saved it into a variable "id" and use it for further API requests.有了这个,我可以提取我想要的值并将其保存到变量“id”中,并将其用于进一步的 API 请求。

response
    .then()
    .log().ifValidationFails(LogDetail.ALL, true)  # I NEED TO PRINT REQUEST PARAMS TOO IF IT FAILS
    .assertThat()
    .statusCode(200)
    ;

If I did the above approach, I couldn't get to validate the response body since we didn't save the response to a variable.如果我采用上述方法,我无法验证响应主体,因为我们没有将响应保存到变量。

I want to do both in one go. Also, I tried the opposite too, where I validated the response body first, but I couldn't save the "id" since we can't convert ValidatableResponse to Response or vice versa.我想在一个 go 中同时执行这两项操作。另外,我也尝试了相反的方法,我首先验证了响应主体,但我无法保存“id”,因为我们无法将 ValidatableResponse 转换为 Response,反之亦然。 I do not want to print the request params unless the request fails.除非请求失败,否则我不想打印请求参数。

Help me find a way with this.帮我想办法解决这个问题。

As far as I understand, you want both: validate response and extract value (for other API) in one chain command.据我了解,您需要两者:在一个链命令中验证响应和提取值(对于其他 API)。 This is example:这是例子:

int id = given().post()
        .then()
        .log().ifValidationFails()
        .assertThat()
        .contentType(ContentType.JSON)
        .statusCode(200)
        .body("id", equalTo("test"))
        .extract()
        .response()
        .path("id");

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

相关问题 如何验证 Json 中的响应值 Rest Assured - how to validate Json Response value in Rest Assured 放心:从响应列表中提取值 - Rest Assured: extract value from Response List Rest-Assured - 如何使用 POJO 验证响应正文 - Rest-Assured - How to validate response body with POJO 如何将访问令牌值从一个 API 响应主体(在一个类中)提取到另一个 API 标头(在另一个类中)在 rest 保证代码中 - How to extract accesss token value from one API response body(in one class) into another API header(in another class) in rest assured code 从 rest 中提取一些字段保证响应 - extract some field from rest assured response 如何使用通用代码从 REST Assured 响应中提取 cookie - How to extract cookies from a REST Assured response using common code 验证 rest 保证 + java 中的多个响应主体节点(键的值) - Validate multiple response body nodes (values for keys) in rest assured + java 如何使用放心从具有多个命名空间的 SOAP XML 响应中提取价值? - How to extract value from SOAP XML response with multiple namespaces using Rest-assured? 放心 - 如何验证具有相同名称的 JSON 响应字段 - Rest assured - How to validate JSON response field with the same name 如何使用带有JSON响应的Rest Assured声明基于正文项? - How to assert based on body item with Rest Assured with JSON response?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM