首页 > 解决方案 > 如何将 JSON GET 请求转换为 HTTP PATCH 请求

问题描述

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

目前我正在通过 GET 方法获取所有信息。现在我想keepForever使用 HTTP PATCH 方法从 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);

                   }
                }
        }
     }
    }
}

标签: javajsonpostpatch

解决方案


您可以使用以下内容来构建 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;
  }

推荐阅读