1

After trying other solutions from HttpURLConnection Invalid HTTP method: PATCH

I am getting Invalid HTTP Method: PATCH Exception with JAVA 7. Updating JAVA is not in option so i have to stick to the workarounds.

I am using Invocation to invoke the request like this

Invocation invoke = reqBuilder.build(getHTTPVerb(), Entity.entity(requestJSON, MediaType.APPLICATION_JSON));
getWebTarget().request(MediaType.APPLICATION_JSON).header("Authorization", getAuthorization()).accept(MediaType.APPLICATION_JSON);

getHTTPVerb() returns String "POST" or "PATCH".

With PATCH method I am having problem.

In the mentioned question, i have not tried one solution with:

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

conn is HttpURLConnection instance.

But I am not sure how I can get HttpURLConnection from Invocation class or any property.

Any pointers or help would be highly appreciated.

user207421
  • 305,947
  • 44
  • 307
  • 483
lesnar
  • 2,400
  • 7
  • 41
  • 72
  • Did you try the solution in http://stackoverflow.com/questions/25163131/httpurlconnection-invalid-http-method-patch that proposes to use HttpClient from httpcomponents? See also http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/methods/HttpPatch.html – Redlab Feb 03 '17 at 14:49
  • 5
    `HttpURLConnection` just doesn't support `PATCH`. You'll have to use a different HTTP library. – OrangeDog Feb 03 '17 at 14:49
  • @Redlab yes i tried , i have also mentioned the same Post in my question. – lesnar Feb 03 '17 at 14:52
  • HttpPatch from httpcomponents would do the trick though.... – Redlab Feb 03 '17 at 14:59

1 Answers1

0

An example of PATCH method with apache http client:

    try {

        //This is just to avoid ssl hostname verification and to trust all, you can use simple Http client also
        CloseableHttpClient httpClient = HttpClientBuilder.create().setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, TrustAllStrategy.INSTANCE).build())
                .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build();

        HttpPatch request = new HttpPatch(REST_SERVICE_URL);
        StringEntity params = new StringEntity(JSON.toJSONString(payload), ContentType.APPLICATION_JSON);
        request.setEntity(params);
        request.addHeader(org.apache.http.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
        request.addHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
        request.addHeader(HttpHeaders.AUTHORIZATION, OAuth2AccessToken.BEARER_TYPE + " " + accessToken);
        HttpResponse response =     httpClient.execute(request);            

        String statusCode = response.getStatusLine().getStatusCode();

    } catch (Exception ex) {
        // handle exception here
    }

Equivalent example with RestTemplate:

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.add("Authorization", OAuth2AccessToken.BEARER_TYPE + " " + accessToken);
    final HttpEntity<String> entity = new HttpEntity<String>(JSON.toJSONString(payload), headers);
    RestTemplate restTemplate = new RestTemplate();
    try {
        ResponseEntity<String> response = restTemplate.exchange(REST_SERVICE_URL, HttpMethod.PATCH, entity, String.class);
        String statusCode =  response.getStatusCode();
    } catch (HttpClientErrorException e) {
        // handle exception here
    }
Dezso Gabos
  • 2,297
  • 5
  • 21
  • 29