Httpurlconnection Invalid Http Method: Patch

HttpURLConnection Invalid HTTP method: PATCH

Yes there is workaround for this. Use

X-HTTP-Method-Override

. This header can be used in a POST request to “fake” other HTTP methods. Simply set the value of the X-HTTP-Method-Override header to the HTTP method you would like to actually perform.
So use following code.

conn.setRequestProperty("X-HTTP-Method-Override", "PATCH");
conn.setRequestMethod("POST");

Getting error while using PATCH method in HttpURLConnection

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

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.

How to make patch http request in groovy

old java HttpUrlConnection.setRequestMethod() does not support patch method:

https://docs.oracle.com/javase/10/docs/api/java/net/HttpURLConnection.html#setRequestMethod(java.lang.String)

public void setRequestMethod​(String method) throws ProtocolException

Set the method for the URL request, one of:
GET
POST
HEAD
OPTIONS
PUT
DELETE
TRACE

however there is a trick - in groovy you could set protected property value and there is a property method

https://docs.oracle.com/javase/10/docs/api/java/net/HttpURLConnection.html#method

so you could change the code:

def body = [test:123]
def post = new URL("http://httpbin.org/patch").openConnection();
post.method ="PATCH";
post.setDoOutput(true);
post.setRequestProperty("Content-Type", "application/json");
post.getOutputStream().withWriter("UTF-8"){ it << new groovy.json.JsonBuilder(body) }
def postRC = post.getResponseCode();
println "Status code = ${postRC}"
println post.getInputStream().getText("UTF-8")


Related Topics



Leave a reply



Submit