JAVA使用Hutool工具类实现DELETE请求的调用
1、Postman请求示例
- Params:包含nonce、timestamp参数
- Header:包含鉴权签名,此处省略系统内部签名获取方式
- Body:包含node_id、user_id
- Postman返回结果
2、maven依赖
- hutool依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.0.6</version>
</dependency>
- JSONObject依赖
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.2.2</version>
<classifier>jdk15</classifier>
</dependency>
3、代码实现
使用hutool工具类,来实现DELETE请求的接口调用,以下是代码实现,部分接口地址及参数因涉及隐私问题,已做处理。
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import net.sf.json.JSONObject;
/**
* @author zyq
* @description DELETE调用请求
* @create 2024-03-26 10:52
*/
public class IdmeshRequest {
public static void main(String[] args) {
//从节点移除用户接口-测试
String url = "https://console.xxx.xxx/api/v1/tree-users?nonce=a1i80lj8uv1aarkj8g7o×tamp=1711420695172";
//封装json请求body
JSONObject jsonObject = new JSONObject();
jsonObject.put("node_id", "1742xxxxxxxxxxxxxxx936");
jsonObject.put("user_id", "174xxxxxxxxxxxxxx12480");
System.out.println(jsonObject);
// 创建 DELETE 请求
HttpRequest request = HttpUtil.createRequest(Method.DELETE, url)
.header("X-Idmesh-Signature","XdCxxxxxxxxxx4Ysp7jG$sm3$a84839c7a71xxxxxxxxxxxxd8b4e7b0da2a1axxxxxxxxxxxx0706a6f")
.body(jsonObject.toString());
// 执行请求并获取响应
HttpResponse response = request.execute();
// 输出响应内容
System.out.println(response.body());
}
}