Rolly Ferolino
Rolly Ferolino

Reputation: 101

Angularjs $resource DELETE not passing data in the body of the request

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.

Upvotes: 2

Views: 2067

Answers (1)

Liran Barniv
Liran Barniv

Reputation: 1376

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;
   }
}

Upvotes: 2

Related Questions