一键导入
java-http-curl-log
Add HTTP non-200 warning logging with curl commands in Java HTTP client classes — helper method, per-call invocation, header/body inclusion
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Add HTTP non-200 warning logging with curl commands in Java HTTP client classes — helper method, per-call invocation, header/body inclusion
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Add WARN logging with curl command when HTTP response status code is not 200 in SDK/API client classes using UNet
Review and fix common bugs in async Java database write patterns — connection lifecycle, thread safety, thread pool usage, bounded queues, memory control
Audit and fix SQL injection vulnerabilities in Java code — quote escaping, whitelist validation, URL encoding, and string comparison bugs
| name | java-http-curl-log |
| description | Add HTTP non-200 warning logging with curl commands in Java HTTP client classes — helper method, per-call invocation, header/body inclusion |
| source | auto-skill |
| extracted_at | 2026-06-29T11:47:56.555Z |
Java HTTP 客户端类中,每个 HTTP 请求方法(GET/POST/PUT/DELETE)调用后都需要在状态码 != 200 时输出 WARN 日志,并附带一条可直接在终端执行的 curl 命令,便于调试。
在类中引入 slf4j Logger:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// 作为类字段
private static final Logger LOGGER = LoggerFactory.getLogger(ClientSdk.class);
该方法接收 net(或类似的 HTTP 客户端对象)、HTTP 方法、URL 和请求体,在状态码非 200 时输出警告和 curl 命令:
/**
* 当 HTTP 状态码不为 200 时,输出 WARN 日志和对应的 curl 命令
*/
private void logNon200Warning(UNet net, String method, String url, String body) {
int statusCode = net.getLastStatusCode();
if (statusCode == 200) {
return;
}
LOGGER.warn("HTTP status code {} for {} {}", statusCode, method, url);
StringBuilder curl = new StringBuilder();
curl.append("curl -X ").append(method).append(" '").append(url).append("'");
// 添加认证头(根据实际情况调整)
if (StringUtils.isNotBlank(this.userToken)) {
curl.append(" -H 'Authorization: ").append(this.userToken).append("'");
}
// 添加其他已知头部(根据实际情况调整)
if (StringUtils.isNotBlank(this.fromIp)) {
curl.append(" -H 'X-Forwarded-For: ").append(fromIp).append("'");
}
// 添加请求体(带单引号转义,防止 body 中含 ' 破坏 shell 语法)
if (StringUtils.isNotBlank(body)) {
String escaped = body.replace("'", "'\\''");
curl.append(" -d '").append(escaped).append("'");
}
LOGGER.warn("Curl: {}", curl);
}
关键细节:
&, ?, $ 等)被 shell 解释'\'' 转义,这是 bash 单引号字符串中插入单引号的标准手法-H 参数在每个方法中执行 HTTP 请求后,马上调用辅助方法:
// GET 无 body
UNet net = this.createNet();
String rst = net.doGet(url);
this.logNon200Warning(net, "GET", url, null);
// POST/PUT 有 body
String body = msg.getJSONObject("body").toString();
UNet net = this.createNet();
String rst = net.doPost(url, body);
this.logNon200Warning(net, "POST", url, body);
// DELETE 无 body
String rst = net.doDelete(url);
this.logNon200Warning(net, "DELETE", url, null);
// DELETE 有 body
String rst = net.doDelete(url, bodyContent);
this.logNon200Warning(net, "DELETE", url, bodyContent);
如果 body 原本是内联传入的(如 net.doPost(url, msg.optJSONObject("body").toString())),需要先提取为局部变量,使同一 body 对象可用于日志:
// 改前
String rst = net.doPost(url, msg.optJSONObject("body").toString());
// 改后
String body = msg.optJSONObject("body").toString();
String rst = net.doPost(url, body);
this.logNon200Warning(net, "POST", url, body);
null,curl 命令中不包含 -d 参数UNet)的 getLastStatusCode() 返回 -1 或 0(请求未执行),方法应安全返回