【问题标题】:How to convert JSON GET request to HTTP PATCH request如何将 JSON GET 请求转换为 HTTP PATCH 请求
【发布时间】:2018-08-01 07:55:25
【问题描述】:

我想将 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);

                   }
                }
        }
     }
    }
}

【问题讨论】:

  • 这里有什么问题?
  • @pvpkiran 如何将上述代码从 GET 转换为 HTTP 补丁方法
  • 你需要做一个小得多的例子,而不是把你所有的代码都放在这里。删掉所有你不需要的额外代码,只给出你遇到问题的代码的最小示例。但更好的是,还有很多从 Java programcreek.com/java-api-examples/… salesforce.stackexchange.com/questions/54870/… 调用补丁的例子,这两个都是在搜索“java call patch”或“java call http patch”时发现的

标签: java json post patch


【解决方案1】:

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

【讨论】:

  • 给出此错误将“PATCH”请求发送到 URL:tfs.tpsonline.com/IRIS%204.0%20Collection/Main/_apis/build/…java.net.ProtocolException:无效的 HTTP 方法:线程“main”中的 PATCH 异常 java.lang.NullPointerException at Test_URL_Req.main(Test_URL_Req .java:148)
  • 给出错误无效的HTTP方法:PATCH
猜你喜欢
  • 1970-01-01
  • 2019-01-20
  • 1970-01-01
  • 2013-06-10
  • 1970-01-01
  • 2019-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多