0

I have a problem with my Patch Request with OpenShift Api. This is my actual code :


public static  HttpStatus PatchHTTPRequestCustomHeaders(String url, String data) {

        String Bearer = "ayJhbGciOiJSUzI1NiIs...";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);     
        headers.set("Authorization", "Bearer "+Bearer);
        headers.set("X-HTTP-Method-Override",  "PATCH");
        headers.set("Content-Type", "application/json-patch+json");
        headers.set("Accept", "application/json");
        final HttpEntity<String> entity = new HttpEntity<String>(data, headers);
        RestTemplate restTemplate = new RestTemplate();

            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class, 1);
            if(response.getStatusCode() == HttpStatus.OK) {
                return HttpStatus.OK;
            }else {
                return null;
            }  
    }

The data contains a op replace or add or remove ...

Error on PostMan : 500 Internal Server Error

On Spring :

2020-02-14 10:12:28.918 ERROR 14944 --- [nio-8169-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException$MethodNotAllowed: 405 Method Not Allowed: [{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"the server does not allow this method on the requested resource","reason":"MethodNotAllowed","details":{},"code":405}
]] with root cause

org.springframework.web.client.HttpClientErrorException$MethodNotAllowed: 405 Method Not Allowed: [{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"the server does not allow this method on the requested resource","reason":"MethodNotAllowed","details":{},"code":405}
]

I don't have any problem with my Patch in Php...

I hope someone know the reason of what is happening.

EDIT :

public static void disableSSLCertificateChecking() {
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            @Override
            public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                // Not implemented
            }

            @Override
            public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                // Not implemented
            }
        } };

        try {
            SSLContext sc = SSLContext.getInstance("TLS");

            sc.init(null, trustAllCerts, new java.security.SecureRandom());

            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

1 Answers1

4

I got same problem when sending a Patch request. Here is the solution for this:

restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory())
Tran Ho
  • 1,442
  • 9
  • 15
  • 1
    Thanks it seems that it helps, but i use my own method `disableSSLCertificateChecking()` based on `TrustManager` in order to do not check the certificate. This method was working fine before i put your command. Now the certifcate error re appear. –  Feb 14 '20 at 09:54
  • I just added the cert into java and the original error came back.. (i added your code.. ) –  Feb 14 '20 at 11:17
  • 1
    Your response if a part of the answer, so i validate it, ( i need to modify from `HttpMethod.POST` to `HttpMethod.PATCH` ) –  Feb 14 '20 at 12:20