2

I have the following code:

deleteArticle = function(assetData) {
   var defer;
   var assetId = assetData.assetId;
   var cseService = $resource(CSE_CONFIG.apiBaseUrl + 'articles/' + assetId,{},   {'remove': {method: 'DELETE', isArray: false}});

   defer = $q.defer();
   $log.info(assetData.data);
   cseService.remove(assetData.data, function(results) {
       return defer.resolve(results);
   }, function(results) {
      $log.error('suggestionsService deleteArticle error', results);
      return defer.reject(results);
   });

   return defer.promise;
}

assetData = {"assetId":12345,"data":{"inappropriate":true,"comment":"This is the comment"}}

I am using express as the server and this is my route:

app.delete('/api/v1/articles/:assetId', function (req, res) {
        console.log("delete is called for " + req.params.assetId);
        console.log(req.body);
        for (var i = 0; i < suggestions[0].articles.length; i++) {
            var row = suggestions[0].articles[i];
            if (row.assetId === req.params.assetId) {
                suggestions[0].articles.splice(i,1);
                console.log("REMOVED:::::");
                console.log(row);
                return res.send("OK",200);
            }
        }
        return res.send("BAD Request",400);
    });

When I send this to the server, the req.body does not contain the assetData.data. So the question is, how do I send the body using the $resource with method=DELETE? Note: I tested the server using Chrome:Postman REST client and it is working properly but not when using angular.

Rolly Ferolino
  • 101
  • 2
  • 9
  • 1
    After searching some more, it appears that sending a body in the request for DELETE method using the $resource is not possible. What I noticed is that what I am trying to pass is converted to query string. Looking at $resource source the body is allowed in POST, PUT, or PATCH but not delete. I guess I have to use $http service. – Rolly Ferolino Sep 12 '13 at 00:21
  • Possible duplicate of [angular $resource delete won't send body to express.js server](http://stackoverflow.com/questions/22186671/angular-resource-delete-wont-send-body-to-express-js-server) – MarCrazyness Jan 06 '17 at 19:25

1 Answers1

2

You can modify the default behavior of each request of the $resource. Check into $resource tranformRequest

Example:

var myResource = $resource(url, params, {
   'delete' : function(data,headers) {
       var myData = { bob: 'bob' };
       return myData;
   }
}
Liran Barniv
  • 1,320
  • 1
  • 10
  • 10