如何将JSON GET请求转换为HTTP PATCH请求

时间:2018-08-01 07:55:25

标签: java json post patch

我想将HTTP GET请求转换为HTTP PATCH请求。我正在访问TFS API,并且想通过使用补丁程序请求自动锁定构建。

目前,我正在通过GET方法获取所有信息。现在,我想使用HTTP PATCH方法将keepForever从false更新为true。通过GET方法,我可以做到这一点,但是现在我必须通过HTTP Patch方法来做到这一点。

有人可以帮我将以下代码从GET方法转换为POST方法吗?

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

                   }
                }
        }
     }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用以下内容构建PATCH请求。但是,您还应该确保服务器通常不支持PATCH。

 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;
  }