0

I'm in a confused trouble with AngularJS and my REST API (Java). I've created a tree-view-drag-drop directive, it has a function to select its items and then delete them, but when I perform a DELETE action using $resource, AngularJS overrides or ignores the request body that is where I send the items of my selection array, how can I solve it? Is there any other patterns that I can use? Maybe some modification in API... or I don't know I'd like some suggestions about this problem and how to solve in the best way both in backend and frontend.

UPDATE

JSFiddle: http://bit.ly/1QmG83Z

2
  • Can you update your post with a mcve? Commented Mar 2, 2016 at 17:36
  • Please, check my answer. Commented Mar 3, 2016 at 10:27

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

But, if my selection contain several items(i.e. 50), wouldn't it too much requests to do?
@AlexStef, if, by some reason, the OP wants an atomic delete, that second suggestion tends to not be the best approach.
@aribeiro Totally agree, that is why i would suggest a new endpoint using (for example) url parameters that will process the complete deletion
0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.