1

I have a case where user can select specific entities with checkbox to be deleted. When user selects specific entity it's id is added to a specific array. For example if I would select first, second and fourth entities their id's would be:

[0, 1, 3]

In front-end service I use following method:

deleteEntities: function(batchIds){
    return $http.delete('/finance/entities', {params: [batchIds collection here??]}).success(function(data){
        return data;
    });
}

I would pass this data to back-end service. Which uses following method:

@Component
@Path("/entities")
@Produces("application/json")
public class FinanceEntityServiceImpl implements FinanceEntityService {
    @DELETE
    public void massDeleteEntitiesByIds(String batchIds){
        System.out.println("batch ids: " + batchIds);
        List<Long> idList = GsonProcessing.deserializeIdsList(batchIds);
        financeDao.massDelete(idList);
    }
}

When I run above code

I have been searching on the net quiet while on how to delete collection resource with angularjs $http and I haven't found yet.

I'm also not sure whether I'm able to put fresh collection to $http.delete as parameter where it says [batchIds collection here??].

What I know this far about deleting collection is that the url should be in form of '/myURL/batch?id=1&id=2&id=3' and correct me if I'm wrong. The problem here is that what if I want to delete a batch of 20 ids, it doesn't make any sense to put all id's in same url.

So my question is what is best way and how to delete collection resources using angularjs $http.delete?

Amir
  • 1,031
  • 2
  • 19
  • 42
  • Is this any help to you http://stackoverflow.com/questions/19957858/send-array-via-get-request-with-angularjs-http-service ? You will have to replace get with delete. – Mudasser Ajaz Aug 30 '15 at 15:23
  • You can look at my post, I think it may help you: http://stackoverflow.com/questions/35754028/angularjs-and-rest-perform-delete-action-sending-array-of-many-items-to-remove/35843348#35843348 – Renan Oliveira Mar 07 '16 at 14:28

2 Answers2

0

First of all, Sending list of ids is a good way to delete with rest api

Second, you shouldn't pass the ids in the url (for security reasons), the better way is to send json inside the body of the request which contain the data you wolud like to send (this is also true for PUT and POST)

Third, if your code always send json (could be forced by creating service which send http request to your api in the same way) your method on the backend should receive json and not string also the parsing (deserlize) of the request shouldn't happend on the method.

Oren Haliva
  • 351
  • 3
  • 14
  • Thanks Oren. What I have done is that when I use JSON as a String I will deserialize it in backend using Gson. I have done specific method to deserialize it. I'm going to try adding json inside the body of the request and add comments accordingly. – Amir Sep 04 '15 at 10:58
  • If it's https, then sending ids in the url isn't a security concern. REST uses verbs (DELETE for deletion). If the data were to be sent in the body, then delete verb can't be used. Then this will not be restful. How do you decide to use delete verb and when to use post/put verb with ids in the body? – Henry Zou Jan 16 '16 at 14:18
0

Okay, the answer was much more simpler than I thought. All I had to do is change params with data:

{params: [batchIds collection here??]} 

into this

{data: batchIds}

And now you are able to delete batch of ids or anything you want providing that you deserialize the data in backend.

Amir
  • 1,031
  • 2
  • 19
  • 42