0

This would be a duplicate, but I really need a good solution. I have tried to research but failed to find anything. Not to complicate, I know it is easy if I just used a GET method, but I needed to use DELETE method. Using DELETE would be easy for the api that I'm sending the request to directly call its Delete method.

I know I should not set the Body but, how I'am able to send the id to the api?

Code Snippet:

HttpRequest req = new HttpRequest(); 
HttpResponse res = new HttpResponse(); 
Http http = new Http(); 

req.setEndpoint('testsite.com/api/User'); 
req.setMethod('DELETE'); 
req.setBody('29'); 
req.setHeader('Content-Type', 'application/json'); 

try { 
  res = http.send(req); 
} 
catch(System.CalloutException e) { 
  System.debug('Callout error:' + e); 
} 

System.debug(res.getBody());
2
  • Why would you want to hardcode a record Id? – Gaurav Kheterpal Feb 12 '15 at 10:27
  • it's being hardcoded because it's just an example. – rpm07 Feb 12 '15 at 12:44
2

You can pass the id for the record to be deleted in the request url by appending the id in the end:

?accountId=00128000001v6vV

and retrive in apex webservice delete method as:

RestContext.request.params.get('accountId')

Something similar should be available for the other API as well.

3
  • Could also set the endpoint to testsite.com/api/User/29 ? More standard in REST APIs to do it this way. – Richard Durrant Feb 12 '15 at 11:38
  • would it more vulnerable when it comes in security, setting it to the endpoint? – rpm07 Feb 12 '15 at 12:45
  • As far as security is concerned, both POST and DELETE are almost same. If data is transferred over secure HTTP the chances of data breach are minimal. Few things that can effect in either case are: POST - size could affect, as the data packets are big DELETE - as id is passed through URL, it could be logged in client browser as cache etc. – CloudConnect Feb 12 '15 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.