AngularJS And REST: Perform DELETE Action Sending Array Of Many Items To Remove
Solution 1:
As far as I know, HTTP method DELETE
doesn't take a body.
You would need an endpoint in your api to treat this "batch" request using an array in body
Or you could also launch a DELETE request on each resource via Angular without any body
Solution 2:
Perhaps the best approach should be creating a POST request, where you would pass your array, and then you could treat the delete atomically:
Service
MyService.$inject = ['$resource'];
function MyService($resource) {
return $resource('/echo/json', {}, {
remove: {
method: 'POST'
}
});
Call
MyService.remove(categoriesToDelete, function(response) {
console.log(response);
// do something after deletion
}
REST method example
@POST
@Path("/json")
@Consumes(MediaType.APPLICATION_JSON)
public Response delete(final String array) {
// You can convert your input to an array and then process it
JSONArray responseArray = new JSONArray(array);
System.out.println("Received input: " + responseArray);
JSONObject jsonObject = new JSONObject();
jsonObject.put("Array received", responseArray);
return Response.status(Status.OK).entity(jsonObject.toString()).build();
}
Also, take a look at this post for further enlightenment.
Solution 3:
Well, all the ideas you gave me were great, and helped me to reach a solution, but reading all the answers and other topics the best solution that I found was to group all ID's in a single string separated by spaces, then I pushed to the path variable, and made a DELETE request as a single resource. Then, my endpoint will split the "combined-resource" and retrieve each of them separately to perform a delete action.
Post a Comment for "AngularJS And REST: Perform DELETE Action Sending Array Of Many Items To Remove"