1

I am trying to publish the github release notes via Java REST service. The have achieved the same by writing the sh script and using curl post call.

I have written the code to do POST call using HttpUrlConnection by passing the JsonObject data.

    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();

Expected Result : Release notes should get published on github

Error: {"message":"Invalid request.\n\n\"tag_name\" wasn't supplied.","documentation_url":"https://developer.github.com/enterprise/2.16/v3/repos/releases/#create-a-release"}

Rupesh Patil
  • 109
  • 2
  • 10

1 Answers1

0

According to the GitHub API documentation for creating a release, below is an example requestBody

{
  "tag_name": "v1.0.0",
  "target_commitish": "master",
  "name": "v1.0.0",
  "body": "Description of the release",
  "draft": false,
  "prerelease": false
}

But in your code, you have formed the requestBody in the below format

{
  "data": {
    "tag_name": "v1.0.0",
    "target_commitish": "master",
    "name": "v1.0.0",
    "body": "Description of the release",
    "draft": false,
    "prerelease": false
  }
}

Since all the fields required for the request are in JSONObject values in your code, pass that directly instead of creating another nesting with JSONObject data.

So essentially, replace wr.write(data.toString()); with wr.write(values.toString());

Madhu Bhat
  • 13,559
  • 2
  • 38
  • 54
  • @RupeshPatil Please consider upvoting and accepting my answer as this would show other users that you've found a solution. – Madhu Bhat Jun 09 '19 at 10:54