1

I'm trying to pass in data that can be used by a DELETE request for my endpoint. I've been able to specify and call the endpoint, but I'm not able to pass in the body for said endpoint. Hence the error. My code consists of the following:

$scope.handleDelete = function (data) {
        angular.forEach(data, function (value, key) {
            var deleteRequest = [];

            var deleteUrl = "api/student/" + value + "/work";
            let body = {
                "Id": key
            }
            deleteRequest.push(body);

            $log.info(JSON.stringify(deleteRequest))

            $http.delete(deleteUrl, {data: deleteRequest});
        });
    }

My output for the delete request is the following:

[{"id":"3"}]

I'm unsure what I am missing regarding the $http.delete in order to get this function to work. Any help would be appreciate, thanks.

  • It's likely you need to include the `data` element in the request options - see https://stackoverflow.com/a/53175140/1663821 for an example – MikeS Jul 03 '20 at 13:04

1 Answers1

0

Since, given how you are forming the deleteRequest, I'm assuming your API endpoint expects a list containing an object, basically this [{'id': key}]

$http.delete() returns a promise, and I don't see you resolving that.

I've added some lines to your code, that might be of some help. Also, please verify your API endpoints accepted param data.

I hope it helps!

$scope.handleDelete = function (data) {
    angular.forEach(data, function (value, key) {
            var deleteRequest = [];

            var deleteUrl = "api/student/" + value + "/work";
            let body = {
                "Id": key
            }
            deleteRequest.push(body);

            $log.info(JSON.stringify(deleteRequest))
            
            $http.delete(deleteUrl, JSON.stringify(deleteRequest))
                 .then(function (response) {
                     if(response.data){
                         $log.info("Data Deleted Successfully!");
                     }
                }, function (response) {
                    // debug..Error occured
                    $log.info("Error Occured!");
                    $log.info(response.status);
                    $log.info(response.statusText);
                }
            );
     };
});
      
Bikash
  • 160
  • 1
  • 6