How to call the github API form HttpsURLConnection in java
Asked Answered
E

0

0

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.

Erk answered 16/1, 2017 at 6:27 Comment(8)
Was there no message with the response? Did you try making the request by hand?System
@System I run the same command using curl for a different repo testingProtectedBranch it worked. But I need a java code for it, that's why I wrote the above code, but I cannot figure out what is wrong with it. the response message is Unprocessable EntityErk
That's not the message, that's just the response code translation.System
@System I donot know it, how do we obtain a response message ?Erk
Try reading httpsURLCon.getErrorStream().System
@System this is the output of it sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@26f67b76Erk
That's not how you read an input stream.System
@System sorry about that, here is what the stream reads, {"message":"Invalid request.\n\nFor 'links/1/schema', nil is not an object.","documentation_url":"https://developer.github.com/v3/repos/branches/#update-branch-protection"}Erk

© 2022 - 2024 — McMap. All rights reserved.