使用 Rest Assured 的 HTTP POST 请求(示例)

HTTP POST Request using Rest Assured (Example)

下面的POST方法可以用来运行 HTTP POST 请求在Rest Assured using Java

更好地依赖 GSON

{
    RestAssured.baseURI = API_URL;

    RequestSpecification request = RestAssured.given();

    request.header("Key1", "Value1");
    request.header("Key2", ""+Value2+""); //If value is getting capture from other variable

    JSONObject requestParams = new JSONObject();
    requestParams.put("Payload Key1", "Payload Value1"); 
    requestParams.put("Payload Key2", "Payload Value2");

    request.body(requestParams.toString());
    Response response = request.post(""); //last word of URL
    int StatusCode = response.getStatusCode(); //Get Status Code
    System.out.println("Status code : " + StatusCode);       
    System.out.println("Response body: " + response.body().asString()); //Get Response Body
}

可以将此代码用于 POST 请求,放心 - Java

如果您正在使用 Rest Assured 进行 API 测试。您必须将 contentType 指定为 "application/json" 并且在 post 方法中您需要传递端点。