Fahad Haroon
Fahad Haroon

Reputation: 25

How to convert JSON GET request to HTTP PATCH request

I want to convert a HTTP GET request to a HTTP PATCH request. I am accessing TFS APIs and I want to lock my build automatically by using a patch request.

Currently I am getting all the information by GET method. Now I want to update keepForever from false to true using the HTTP PATCH method. By GET method I am able to do that but now I have to do that by HTTP Patch method.

Can someone help me converting the below code from GET method to POST method?

public class Test_URL_Req {

    public static String getURLResponse(String url) {
        try {
            System.out.println("\nSending 'GET' request to URL : " + url);
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setRequestMethod("GET");
            con.setRequestProperty("Accept", "application/json");
            int responseCode = con.getResponseCode();

            System.out.println("Response Code : " + responseCode);
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));

            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
                // System.out.println(response);
            }
            in.close();
            return response.toString();
        } catch (Exception e) {
            System.out.println(e);
        }
        return null;
    }

    public static void main(String[] args) throws JSONException{
        String url = "https://tfs.tpsonline.com/IRIS%204.0%20Collection/Main/_apis/build/definitions?api-version=4.1";
        //String url1 ="https://tfs.tpsonline.com/IRIS%204.0%20Collection/Main/_apis/build/builds?api-version=4.1&definitions=" + Def_id +"&resultFilter=succeeded&$top=1";

        String response  = getURLResponse(url);
       // String response1  = getURLResponse(url1);

        JSONObject obj_JSONObject = new JSONObject(response.toString());
        JSONArray obj_JSONArray = obj_JSONObject.getJSONArray("value");
        String Def_id=null;
        for(int i=0; i<obj_JSONArray.length();i++)
        {
           JSONObject obj_JSONObject2 = obj_JSONArray.getJSONObject(i);

            String value = obj_JSONObject2.getString("name");

            String toSearch= "DEVOPS";
           if(value.equals(toSearch)){
                System.out.println("STATUS:-");
                System.out.println(value);
                String result =obj_JSONObject2.getString("name");
                System.out.println("BUILD NAME");
                System.out.println(result);
                Def_id = obj_JSONObject2.get("id").toString();
                System.out.println("DEFINATION ID");
                System.out.println(Def_id);

                break;

            }
        }

        if (Def_id != null)
        {
            String url1 ="https://tfs.tpsonline.com/IRIS%204.0%20Collection/Main/_apis/build/builds?api-version=4.1&definitions=" + Def_id +"&resultFilter=succeeded&$top=1";
            String response1  = getURLResponse(url1);

            JSONObject obj_JSONObject1 = new JSONObject(response1.toString());
            JSONArray obj_JSONArray1 = obj_JSONObject1.getJSONArray("value");
            String Build_id=null;

            for(int i=0; i<obj_JSONArray1.length();i++)
            {
               JSONObject obj_JSONObject2 = obj_JSONArray1.getJSONObject(i);

                String value = obj_JSONObject2.getString("result");
                //String value = obj_JSONObject2.get("id").toString();
                //System.out.println(value);

                String toSearch1= "succeeded";
               if(value.equals(toSearch1)){

                    System.out.println("#######################################");
                    System.out.println("RESULT");
                    System.out.println(value);

                    String result =obj_JSONObject2.getString("status");
                    System.out.println("STATUS");
                    System.out.println(result);
                    Build_id = obj_JSONObject2.get("id").toString();
                    System.out.println("BUILD ID");
                    System.out.println(Build_id);


                    //boolean  keepForever =obj_JSONObject2.getBoolean("keepForever");

                   //if(keepForever == false)
                   //{
                   //  keepForever=true;

                   //}

                   // System.out.println(keepForever);

               }
            }
            if (Build_id != null)
            {

                String url2= "https://tfs.tpsonline.com/IRIS%204.0%20Collection/Main/_apis/build/builds?api-version=4.1&buildNumber=" + Build_id;
                String response2  = getURLResponse(url2);

                JSONObject obj_JSONObject2 = new JSONObject(response2.toString());
                JSONArray obj_JSONArray2 = obj_JSONObject2.getJSONArray("value");

                for(int i=0; i<obj_JSONArray2.length();i++)
                {
                   JSONObject obj_JSONObject3 = obj_JSONArray2.getJSONObject(i);

                    String value = obj_JSONObject3.getString("result");
                    //String value = obj_JSONObject2.get("id").toString();
                    //System.out.println(value);

                    String toSearch1= "succeeded";
                   if(value.equals(toSearch1)){
                     boolean keepForever =obj_JSONObject3.put("keepForever", false) != null;

                    if(keepForever == false)
                    {
                        keepForever = true;
                    }
                       System.out.println("#######################################");
                       System.out.println(keepForever);

                   }
                }
        }
     }
    }
}

Upvotes: 0

Views: 1573

Answers (1)

piy26
piy26

Reputation: 1612

You can just use the below to build PATCH request. However, you should also make sure that your server supports PATCH as its generally unsupported.

 public static String getPatchResponse( String url){
      try {
          System.out.println("\nSending 'PATCH' request to URL : " + url);
          URL obj = new URL(url);
          HttpURLConnection con = (HttpURLConnection) obj.openConnection();
          con.setRequestMethod("PATCH");
          con.setRequestProperty("Accept", "application/json");
          int responseCode = con.getResponseCode();

          System.out.println("Response Code : " + responseCode);
          BufferedReader in = new BufferedReader(
                  new InputStreamReader(con.getInputStream()));

          String inputLine;
          StringBuffer response = new StringBuffer();
          while ((inputLine = in.readLine()) != null) {
              response.append(inputLine);
              //System.out.println(response);
          }
          in.close();
        return  response.toString();
      } catch (Exception e) {
          System.out.println(e);
      }
      return null;
  }

Upvotes: 1

Related Questions