在HttpURLConnection中使用PATCH方法时出现错误 [英] Getting error while using PATCH method in HttpURLConnection
问题描述
从最近的几个小时开始,我正在尝试使用 HttpURLConnection
PUT 方法来更新资源的某些字段.但是现在我将其更改为 PATCH .
Here from the last few hours, I am trying to update some field of resource using HttpURLConnection
PUT method. But now I've changed this to PATCH.
我能够执行 GET
和 POST
,但是在Http方法 PATCH
中仍然会出错.
I am able to perform GET
and POST
, but in Http method PATCH
keep getting error.
请求甚至没有在 POSTMAN
中发送.
The request not even being sent in 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();
}
通过覆盖从 POST
在 HttpUrlConnection
中使用 PATCH
方法的想法/questions/25163131/httpurlconnection-invalid-http-method-patch>此处.
The idea of using PATCH
method in HttpUrlConnection
by overriding the POST
got from here.
I got the idea of sending parameters in the request body got from here.
此URL https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/tasks/
上的可用资源就像
The resources available at this url https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/tasks/
are like
{
"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"
}]
}
我正在尝试在此URL https://lavazzaindia--tst1.custhelp.com上使用 PATCH 方法对该资源的某些字段进行
update
./services/rest/connect/latest/tasks/255
And I am trying to update
some field of this resource , using PATCH method at this url 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)
请有人在这里帮助我解决此问题.
Please someone help me out here to fix this.
推荐答案
好,我在回答自己的问题.
Well, I'm answering own question.
我只修改了几行代码,它对我有用.以下是工作代码:
I just modified few lines of code and it worked for me. Following is the working code:
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
中,当URL中已经存在参数 id
时,我们不应该发送它.
In the variable inputJson
we shouldn't send the parameter id
while it's already in the url.
我一直在尝试使用示例程序的数量,最后资源得到了更新.
I was keep trying with the numbers of example programs, and finally resource was getting updated.
这篇关于在HttpURLConnection中使用PATCH方法时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!