我正在尝试通过 Java REST 服务发布 github 发行说明。通过编写 sh 脚本和使用 curl post call 实现了相同的目的。
我已经编写了代码来通过传递 JsonObject 数据使用 HttpUrlConnection 进行 POST 调用。
String postUrl = "host_name/api/v3/repos/"+ userName + "/" + project_name
+ "/releases";
URL url = new URL(postUrl);
JSONObject values = new JSONObject();
JSONObject data = new JSONObject();
values.put("tag_name", "TEST_TAG1");
values.put("target_commitish", "master");
values.put("name", "1.0");
values.put("body", "TEST Description");
values.put("draft", false);
values.put("prerelease", false);
data.put("data", values);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Authorization", "token goes here");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/vnd.github.v3+json");
con.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
wr.write(data.toString());
wr.flush();
wr.close();
预期结果:发行说明应该在 github 上发布
错误:{"message":"无效请求。\n\n\"tag_name\" 未提供。","documentation_url":" https://developer.github.com/enterprise/2.16/v3/repos/发布/#create-a-release "}