How to call the github API form HttpsURLConnection in java

眉间皱痕 提交于 2019-12-12 03:47:16

问题


I need to change my branch named testingProtectedBranch1 as a protected branch with providing the following parameters on

required_status_check : include_admins= true, strict= true, context= continuous-integration/travis-ci

restrictions: null

required_pull_request_reviews: include_admins=false

here is my code and the access token ( the variable token ) is provided by the user at the runtime.

public void setMasterBranchAsProtected() throws Exception{
   String URLForCallingTheBranchAPI="https://api.github.com/repos/kasunsiyambalapitiya/testingProtectedBranch1/branches/master/protection";

  String  jsonInput="{\"required_status_checks\":{\"include_admins\":true,\"strict\":true,\"contexts\":[\"continuous-integration/travis-ci\"]},"
           + "\"restrictions\":null,"
           + "\"required_pull_request_reviews\":{\"include_admins\":false} ";


    try {
        URL urlObject= new URL(URLForCallingTheBranchAPI);

        HttpsURLConnection httpsURLCon= (HttpsURLConnection)urlObject.openConnection();
        httpsURLCon.setDoOutput(true);
        httpsURLCon.setRequestMethod("PUT");
        httpsURLCon.setRequestProperty("User-Agent", "Mozilla/5.0");


        httpsURLCon.setRequestProperty("Accept","application/vnd.github.loki-preview+json");
        httpsURLCon.setRequestProperty("Authorization", "Bearer "+token);


        OutputStreamWriter outputStream= new OutputStreamWriter(httpsURLCon.getOutputStream());


        outputStream.write(jsonInput);

        int responseCode= httpsURLCon.getResponseCode();

        outputStream.flush();
        outputStream.close();






    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



}

but for the response code I receive 422 which resembles unprocessable entity. What I am doing wrong in here, please help me to figure this out. Thanks in advance.

来源:https://stackoverflow.com/questions/41670608/how-to-call-the-github-api-form-httpsurlconnection-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!