Is there a way to pass header information in Spring RestTemplate DELETE call

Zeeshan picture Zeeshan · May 8, 2015 · Viewed 13.2k times · Source

In Spring RestTemplate we are having the following methods for delete.

@Override
    public void delete(String url, Object... urlVariables) throws RestClientException {
        execute(url, HttpMethod.DELETE, null, null, urlVariables);
    }

    @Override
    public void delete(String url, Map<String, ?> urlVariables) throws RestClientException {
        execute(url, HttpMethod.DELETE, null, null, urlVariables);
    }

    @Override
    public void delete(URI url) throws RestClientException {
        execute(url, HttpMethod.DELETE, null, null);
    }

None of these methods are having any place to pass header information. Is there any other method which can be used for DELETE request with header information?

Answer

David Lavender picture David Lavender · May 8, 2015

You can use the exchange method (which takes any HTTP request type), rather than using the delete method:

MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("X-XSRF-HEADER", "BlahBlah");
headers.add("Authorization", "Basic " + blahblah);
etc...

HttpEntity<?> request = new HttpEntity<Object>(headers);
restTemplate.exchange(url, HttpMethod.DELETE, request, String.class);