如何使用Restassured库从请求或响应对象获取POST请求中传递的body(json)的内容?

时间:2018-12-27 19:11:08

标签: post request rest-assured

我正在尝试使用RequestSpecification或Restured library的Response对象读取POST请求的正文中传递的内容。我找不到任何可以返回传递的正文内容的方法。有帮助吗?

public RequestSpecification Request(RequestSpecification rs)
{
    rs = RestAssured.given();
    if(APIConstants.BASEURL.contains("https")) {
        rs = rs.relaxedHTTPSValidation();
        rs.port(443);
    }       
    return rs;
}
public RequestSpecification postRequestBuilder(RequestSpecification rs,String Body)
{
    rs=Request(rs);
    rs = rs.contentType(APIConstants.contentType);
    if(Body!=null) {        
        rs = rs.body(Body); 
    }
    return rs;
}

public Response performPOSTOperation(RequestSpecification rs,String Resource, String TestParameters)
{
    Response rp=null;
    String url=BASEURL+"/"+Resource+"/"+TestParameters;
    System.Out.Println("Request is " +url);
    rp = rs.when().post(url);
           // I am trying to print Request body parameter here.
    System.Out.Println("Response is " +rp.asString());      
    return rp;
}
public void test(){                                                    
            String JSONBody="{"TestNumber":1}";
            RequestSpecification rs;
    rs= postRequestBuilder(rs,JSONBody);
    Response rp= performPOSTOperation(rs,RESOURCE,Service);
}

我正在尝试打印在PerformPOSTOperation()中通过test()传递的JSONBody

1 个答案:

答案 0 :(得分:0)

您可以在请求规范对象中添加.log:

rs.log(LogDetail.BODY);

或添加过滤器-详细信息@ https://static.javadoc.io/io.rest-assured/rest-assured/3.0.6/io/restassured/filter/log/RequestLoggingFilter.html