Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - RestTemplate 405 Method Not Allowed Although Postman Call Works

I am experiencing a weird issue when working with RestTemplate. I'm using a certain REST API and there I want to update something using PUT. Thus, in e.g. Postman I am sending this request:

PUT http://fake/foobar/c/123 with a certain body

This update via Postman is successful. If I now execute the same call in Java via a RestTemplate, I am getting a 405 Method Not Allowed:

HttpHeaders headers = createHeader();
HttpEntity<Offer> httpEntity = new HttpEntity<>(bodyEntity, headers);
String url = "http://fake/foobar/c/123"; //Created dynamically, but here pasted for sake of simplicity
RestTemplate restTemplate = new RestTemplate(...);
ResponseEntity<OfferResponse> response = restTemplate.exchange(url, HttpMethod.PUT, httpEntity, OfferResponse.class);
...

I compared the URL again and again. If I copy the URL logged in the console and copy it to Postman, I can do the update successfully. I also compared the headers and everything. Everything is equal to how it is done via Postman.

Is there any potential other reason for such a behavior (another reason than I am too stupid comparing the headers etc. and missing something)? Other PUT, POST calls etc. against this API are working fine, otherwise I would have assumed that there is a general problem with my usage of RestTemplate

like image 646
MDDCoder25 Avatar asked Sep 08 '25 12:09

MDDCoder25


2 Answers

Code 405 Method Not Allowed means the HTTP verb (GET, POST, PUT, etc.) you use against this end-point is known but not accepted by the API.

If you can't post the details of your API as @Dinesh Singh Shekhawat suggested, I will first try to use Postman Code feature and get an automatically generated code for Java (OkHTTP or UniRest) of the request. You can find this option on the right part below the Send button. Copy this code and try to perform the request.

enter image description here

Then compare this request with yours.

You can always use HttpPut instead of RestTemplate if it's not a requirement:

HttpClient client = HttpClientBuilder.create().build();
String url = "http://fake/foobar/c/123";

HttpHeaders headers = createHeader();
HttpEntity<Offer> httpEntity = new HttpEntity<>(bodyEntity, headers);
HttpPut httpPut = new HttpPut(url);
httpPut.setEntity(httpEntity);

HttpResponse response = client.execute(httpPut);

like image 197
Victor Calatramas Avatar answered Sep 11 '25 01:09

Victor Calatramas


Just in case it helps someone else: I was encountering the same issue and for me it was just the issue of a trailing slash / on the URL. In insomnia (similar to postman) I had a trailing slash, in code I didn't. When I added the slash to my code everything worked.

failure: http://localhost:8080/api/files
success: http://localhost:8080/api/files/

Of course it could also be the other way around, so just double check the actual api definition.

like image 40
JPProgrammer Avatar answered Sep 11 '25 02:09

JPProgrammer