3

I'm using cloudinary to store images. when I try to delete a resource, I get the following error message:

HTTP method DELETE doesn't support output.

my source code:

     Http h = new Http();
     HttpRequest req = new HttpRequest();
     req.setEndpoint('http://api.cloudinary.com/v1_1/'+cloudname+'/image/upload');
     req.setMethod('DELETE');
     String myData = 'public_id=myPublicId';
     Blob hash = Crypto.generateDigest('SHA1',Blob.valueOf(myData));
     String hexDigest = EncodingUtil.convertToHex(hash);
     String finalBodyString='public_id=myPublicId&api_key='+apikey+'&timestamp='+EncodingUtil.urlEncode(tiStmp, 'UTF-8')+'&signature='+EncodingUtil.urlEncode(hexDigest, 'UTF-8');
    req.setBody(finalBodyString);
    HttpResponse res = h.send(req);

2 Answers 2

1

Delete methods in HTTPRequests does not support setBody methods.

I will suggest you to authenticate the request and pass the other necessary parameters for delete operation using the setHeader method.

1
  • I didn't understand. could you please help me by correcting my source code Commented Apr 13, 2015 at 8:10
1

Rather than making a DELETE call to the /image/upload resource, make a POST to the /image/destroy resource and pass the public_id of the image you want to delete in the request body. This should put you on the right track:

    Http h = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint('https://api.cloudinary.com/v1_1/'+cloudname+'/image/destroy');
    req.setMethod('POST');

    String tiStmp = String.valueOf(System.now().getTime() / 1000);
    String myData = 'public_id='+picture.Id+'&timestamp='+tiStmp+apisecret;
    Blob hash = Crypto.generateDigest('SHA1', Blob.valueOf(myData));
    String hexDigest = EncodingUtil.convertToHex(hash);

    String deletetemplate = 'public_id={0}&api_key={1}&timestamp={2}&signature={3}';

    String finalBodyString = String.format(deletetemplate, new String[]{
            'myPublicId',
            apikey,
            EncodingUtil.urlEncode(tiStmp, 'UTF-8'),
            EncodingUtil.urlEncode(hexDigest, 'UTF-8')
        });

    System.debug('request body: ' + finalBodyString);
    req.setBody(finalBodyString);

    HttpResponse res = h.send(req);
    System.debug('res: ' + res);
    System.debug('response body: ' + res.getBody());
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .