说明 Rest Assured 中的 DELETE 请求。
我们可以在 Rest Assured 中执行 DELETE 请求。这是借助于 http DELETE 方法完成的。它负责删除服务器资源。
Delete 请求可以存在请求或响应正文。DELETE 请求可用的状态代码如下 −
- 200 (OK)
- 204(如果我们想要删除的记录没有内容)
- 202(已接受,删除不是单一操作)。
我们首先将通过 Postman 对端点发送一个 DELETE 请求 − http://dummy.restapiexample.com/api/v1/delete/100.

使用 Rest Assured,我们将检查响应正文是否包含字符串已成功!记录已被删除。
示例
代码实施
import org.testng.Assert;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
public class NewTest {
@Test
public void deleteRequest() {
int record = 100;
//base URI with Rest Assured class
RestAssured.baseURI ="https://dummy.restapiexample.com/api/v1/";
//input details
RequestSpecification r = RestAssured.given();
//request header
r.header("Content-Type", "application/json");
//capture response from Delete request
Response res = r.delete("/delete/"+ record);
//verify status code of Response
int s = res.getStatusCode();
Assert.assertEquals(s, 200);
//convert response to string then validate
String jsonString =res.asString();
Assert.assertEquals
(jsonString.contains("Successfully! Record has been deleted"), true);
}
}输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP