2

I am implementing a Spring client for an existing REST API and I need to invoke a DELETE while, at the same time, passing an access token in the request body, like this:

{ 
    "access_token": "..."
}

The problem is that, using the method that works for POST, the transmitted body is empty (I have intercepted the request body and made sure) and I cannot be authorised without this access token. This is what I am doing:

 RestTemplate restTemplate = new RestTemplate();
 UserRequest ur = new UserRequest(access_token);
 HttpEntity<UserRequest> entity = new HttpEntity<>(ur);                                               
 restTemplate.delete(url, entity);

I have no control over the API itself, so I don't have the option of passing the token as url parameter.

Is there a way to do this in Spring, or do I have to build my own HttpUrlConnection like described for instance in this SO answer?

Community
  • 1
  • 1
ACEG
  • 1,981
  • 6
  • 33
  • 61
  • Cristina, Please, write the sample code how have you resolved it. – Taras Vovkovych Feb 16 '18 at 14:42
  • @TarasVovkovych Hmm, I'm afraid in the meantime I have another job, in another country, with other tools...but I'll check, perhaps I still have some backup code somewhere. I'm sorry! – ACEG Feb 19 '18 at 12:26

1 Answers1

4

In the RestTemplate object in Spring there's an exchange method.

The parameters are :

  • the url
  • the method, in your case HttpMethod.DELETE
  • the entity (with the body you have to transmit)
  • the response type
  • some object you could pass

Hope this help

vincent
  • 1,214
  • 12
  • 22
  • Thanks, I will try it out! – ACEG Sep 18 '15 at 09:19
  • 1
    Been trying the template.exchange(url, HttpMethod.DELETE, requestEntity, String.class) - doesn't work - the server doesn't see the income body, the delete(...) method doesn't work also – Taras Vovkovych Feb 16 '18 at 14:47