Sending id to DELETE API using $resource in angularJS
Asked Answered
J

2

7

I have this delete method with $resource for my angularJS service,

return $resource(apiUrl, {}, {
    delete: {
        url: apiUrl+ ":itemId",
        params: { itemId: "@itemId" },
        method: 'DELETE'
    }
});

However while trying to call the delete method, the request which sends from the service is without itemId.

dataService.delete({ itemId: itemId}, {})
                .$promise
                .then((data) => {
                    // Statements
                })
                .catch((error) => {
                    // Logging
                })
                .finally(() => {});

The URL supposed to call is https://url:someport/v1.0/items/{itemId} However the current url is https://url:someport/v1.0/items

Is there a way to get this doe using the current $resource code?

Jingo answered 19/7, 2018 at 10:49 Comment(0)
D
0

I think you need to use second parameter to specify params for the request.

return $resource(apiUrl, {itemId: '@itemId'}, {
delete: {
    method: 'DELETE'
}
});

You can see entire documentation over here https://docs.angularjs.org/api/ngResource/service/$resource . This will give you better idea. You need to provide params in second argument (object).

var User = $resource('/user/:userId',
{userId: '@id'},
{delete: {
    method: 'DELETE' }
});

User.delete({userId: 123}).$promise.then(function(user) {
  // perform other operations
});
Detective answered 23/7, 2018 at 6:34 Comment(5)
Well I don't have a single default ID to be used . This ItemID is particularly different from other Id. So I am looking at specifically having that Info passed into delete methodJingo
@Jingo I didn't get it. I think you can pass as many params as you want in params object.Detective
Well I already have a default param, id in there. Which is used by all PUT, POST GET etc. So is it finen to pass itemId as well? $resource(apiUrl, {Id: '@Id'}, {Jingo
@Jingo Yes, you can use any number of params. I've written {userId: '@id'} just for example. You can try using other params.Detective
Thanks Let me try that outJingo
P
1

This code sends the proper url:

angular.module("app",['ngResource'])
.controller("ctrl",function($scope,$resource){
  var url = "//requestbin.fullcontact.com/1ooe8eb1";
  var dataService =  $resource(url, {itemId: "@itemId"}, {
    delete: {
        url: url+"/items/:itemId",
        params: { itemId: "@itemId" },
        method: 'DELETE'
    }
  });
  $scope.delete = function() {
    var res = dataService.delete({itemId:77});
    res.$promise.finally(function(){
      $scope.result="DONE";
    });
  };
});
<script src="//unpkg.com/angular/angular.js"></script>
<script src="//unpkg.com/angular-resource/angular-resource.js"></script>
<body ng-app="app" ng-controller="ctrl">
    <button ng-click="delete()">Delete Item</button>
    {{result}}
</body>

The response is blocked by the browser because of CORS but inspection at:

https://requestbin.fullcontact.com/1ooe8eb1?inspect

shows that the URL was formed correctly.

Protecting answered 28/7, 2018 at 22:31 Comment(0)
D
0

I think you need to use second parameter to specify params for the request.

return $resource(apiUrl, {itemId: '@itemId'}, {
delete: {
    method: 'DELETE'
}
});

You can see entire documentation over here https://docs.angularjs.org/api/ngResource/service/$resource . This will give you better idea. You need to provide params in second argument (object).

var User = $resource('/user/:userId',
{userId: '@id'},
{delete: {
    method: 'DELETE' }
});

User.delete({userId: 123}).$promise.then(function(user) {
  // perform other operations
});
Detective answered 23/7, 2018 at 6:34 Comment(5)
Well I don't have a single default ID to be used . This ItemID is particularly different from other Id. So I am looking at specifically having that Info passed into delete methodJingo
@Jingo I didn't get it. I think you can pass as many params as you want in params object.Detective
Well I already have a default param, id in there. Which is used by all PUT, POST GET etc. So is it finen to pass itemId as well? $resource(apiUrl, {Id: '@Id'}, {Jingo
@Jingo Yes, you can use any number of params. I've written {userId: '@id'} just for example. You can try using other params.Detective
Thanks Let me try that outJingo

© 2022 - 2024 — McMap. All rights reserved.