5

I'm experiencing a strange problem with the method below.

@Override
public String deleteToEe(String body) {     
    logger.debug("Request body");
    logger.debug(body);
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", MediaType.APPLICATION_JSON_VALUE);
    headers.add("partner", "test");
    headers.add("api_key", "certxxxx");
    HttpEntity<String> request = new HttpEntity<String>(body, headers);
    ResponseEntity<String> result = null;
    try {
        result = restTemplate.exchange(targetUrl, HttpMethod.DELETE, request, String.class);
    } catch (RestClientException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return result.getBody();
}

When I trigger this method through hitting the controller request mapping through Postman, it works. But when testers triggers this method through their integration tests, or when I trigger this method using curl

curl -X DELETE -H "Accept: application/json" -H "Content-type: application/json" -d "{"userName": "21", "courseId": "104882_bfaculty3_1024", "isbn": "9780323055", "schoolUserId": "1234" }" http://localhost:8080//api/provision

I get a null pointer exception at this point in the code

result = restTemplate.exchange(targetUrl, HttpMethod.DELETE, request, String.class);

I've breakpointed the code and looks like we have a request body but for some reason it' being dropped at the restTemplate.exchange() call. Anyone seen something like this before?

user619804
  • 2,286
  • 12
  • 46
  • 71
  • Is dropped the body that goes to deleteToEe method or is it present for all cases (CURL, controller...)? – zdenda.online Oct 14 '15 at 16:36
  • Passing Request Body in DELETE methods is support from version 3.0.5 https://jira.spring.io/browse/SPR-7867 – Arashsoft Feb 12 '18 at 20:44
  • Can you also post the exception stacktrace? as @Arashsoft says, this should've already been supported since long ago. – Bustanil Arifin Oct 01 '19 at 03:12
  • Go to postman top and open console and add console logs into question – vaquar khan Sep 27 '20 at 04:51
  • Read this: https://stackoverflow.com/questions/25375046/passing-data-in-the-body-of-a-delete-request – DwB May 25 '23 at 19:17
  • Another link: https://stackoverflow.com/questions/36899641/resttemplate-delete-with-body The second link notes that "this is fixed after 4.0.x" – DwB May 25 '23 at 19:23

1 Answers1

0

You should not have any body in the request when using HTTP DELETE method. Many frameworks discourage using it or warn you that the body may be dropped. The reason is that you want to DELETE some resource identified by your URI thus no body should be required.

It sounds like this may be your case. It would also explain why some tools do send the body and why others doesn't.

I would strongly recommend you to re-design your API to something similar to

DELETE http://localhost:8080/api/provision/{id}

or different URI dependent on how your data (resources) are designed

zdenda.online
  • 2,451
  • 3
  • 23
  • 45
  • The problem is I am working off of requirements and am not sure I'm able to change the design, as we are sending the request to an external api that requires certain information in the body - redesigning the api so that all of this information is encapsulated in the url would result is a really long url. – user619804 Oct 14 '15 at 16:18
  • The question is whether all the information should be in URI. Maybe you may have some internal database (cache) that holds metadata. Anyway if I understood well, the external API requires body in HTTP DELETE too. Maybe you can give a try to another framework (instead of RestTemplate) to invoke HTTP call to this exteternal API. E.g. simple Java's HttpUrlConnection to find out if the problem persists.. But I undestood that the problem stands in the client. Meaning that CURL and integration tests are dropping body for HTTP DELETE and Postman does not – zdenda.online Oct 14 '15 at 16:30
  • This is not an answer to this question, it just explains what happens. It should be a comment. – Arashsoft Feb 12 '18 at 20:39