【问题标题】:Getting error while using PATCH method in HttpURLConnection在 HttpURLConnection 中使用 PATCH 方法时出错
【发布时间】:2017-08-03 11:27:03
【问题描述】:

从最近几个小时开始,我正在尝试使用HttpURLConnection PUT 方法更新一些资源字段。但现在我已将其更改为 PATCH

我能够执行GETPOST,但是在Http 方法PATCH 中不断出现错误。

请求甚至没有在POSTMAN 中发送。

这是java 类:

try {
    String serUrl = "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/tasks/255";
    String authString = user + ":" + password;
    byte[] authEncBytes = Base64.getEncoder().encode(authString.getBytes());
    String authStringEnc = new String(authEncBytes);

    URL url = new URL(serUrl); //Enter URL here
    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
    httpURLConnection.setDoInput(true);
    httpURLConnection.setDoOutput(true);
    httpURLConnection.setRequestProperty("X-HTTP-Method-Override", "PATCH");
    httpURLConnection.setRequestMethod("POST");
    httpURLConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
    httpURLConnection.setRequestProperty("Content-Type", "application/json");

    httpURLConnection.connect();

    String inputJson = "{   \"id\":" + 255 + "," +
        "\"assignedToAccount\": {" +
        "     \"id\":" + 233 +
        " }," +
        " \"name\":\"" + "task2_checking34" + "\"," +
        " \"serviceSettings\":{" +
        "     \"incident\":{" +
        "         \"id\":" + 380 +
        "     }" +
        " }" +
        "}";
    OutputStreamWriter osw = new OutputStreamWriter(httpURLConnection.getOutputStream());
    osw.write(inputJson);
    osw.flush();
    osw.close();

    BufferedReader br = new BufferedReader(new InputStreamReader(
        (httpURLConnection.getInputStream())));

    String output;
    StringBuffer bfr = new StringBuffer();
    String res = "";
    System.out.println("Output from Server .... \n");
    while ((output = br.readLine()) != null) {
        bfr.append(output);
    }
    res = bfr.toString();

} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

HttpUrlConnection 中使用PATCH 方法的想法是通过覆盖从here 获得的POST

我想到了在来自here的请求正文中发送参数。

此网址https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/tasks/ 上可用的资源类似于

{
    "id": 253,
    "lookupName": "task_quality34",
    "createdTime": "2017-08-03T05:34:34Z",
    "updatedTime": "2017-08-03T05:34:34Z",
    "links": [{
        "rel": "canonical",
        "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/tasks/253"
    }]
}, {
    "id": 255,
    "lookupName": "task_quality-test12",
    "createdTime": "2017-08-03T05:48:26Z",
    "updatedTime": "2017-08-03T05:48:26Z",
    "links": [{
        "rel": "canonical",
        "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/tasks/255"
    }]
}

我正在尝试update 这个资源的某个字段,在这个 url 使用 PATCH 方法https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/tasks/255

但是每次我都会出错

java.io.IOException: Server returned HTTP response code: 400 for URL: https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/tasks/255
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
    at com.cloud.task.TaskUpdate.main(TaskUpdate.java:80)

请有人帮我解决这个问题。

【问题讨论】:

  • 此服务是否记录在任何地方?只有服务可以告诉如何构建正确的请求
  • 我已经使用HttpUrlConnection 成功构建了GETPOST 请求。但是在PUT 方法中,请在代码中指导我是否做错了。
  • 参考我上面的评论。我不知道 HTTP 端点期望什么
  • @SNSingh, 400 Bad request 主要是通过 url 编码,但是你说你已经成功完成了 GET 和 POST 方法调用,然后我怀疑 json 中的参数丢失或添加了额外的您正在发送 PUT。
  • @nandsito 好的,我已经添加了端点可用的资源,并在添加 id 后输入其中的任何一个。我可以使用POST 创建新资源,但现在无法更新现有资源。

标签: java httpurlconnection http-status-code-400 http-patch


【解决方案1】:

好吧,我在回答自己的问题。

我刚刚修改了几行代码,它对我有用。 以下是工作代码:

        try {
            URL url = new URL(serUrl); //Enter URL here
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
            httpURLConnection.setRequestProperty("Accept", "application/json");
            httpURLConnection.setRequestProperty("Content-Type", "application/json");
            httpURLConnection.setRequestProperty("X-HTTP-Method-Override", "PATCH");
            httpURLConnection.connect();

            DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
            wr.write(inputJson.getBytes());
            wr.flush();
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (httpURLConnection.getInputStream())));


            StringBuffer bfr = new StringBuffer();
            String output = "";
            String res = "";

            while ((output = br.readLine()) != null) {
                bfr.append(output);
            }
            resCode = httpURLConnection.getResponseCode();
//            System.out.println("response code = "+resCode);
            if (resCode != 200) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + resCode +"\n"
                        +bfr.toString());
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

在变量inputJson 中,当参数id 已经在url 中时,我们不应该发送它。

我一直在尝试使用示例程序的数量,最后资源得到了更新。

【讨论】:

    猜你喜欢
    • 2014-09-29
    • 2017-01-23
    • 1970-01-01
    • 2015-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    相关资源
    最近更新 更多