tags:

views:

243

answers:

5

In wiki article for REST

it is indicated that if you use http://example.com/resources DELETE, that means you are deleting the entire collection.

If you use http://example.com/resources/7HOU57Y DELETE, that means you are deleting that element.

I am doing a WEBSITE, note NOT WEB SERVICE.

I have a list that has 1 checkbox for each item on the list. Once i select multiple items for deletion, i will allow users to press a button called DELETE SELECTION. If user presses the button, a js dialog box will popup asking user to confirm the deletion. if user confirms, all the items are deleted.

So how should i cater for deleting multiple items in a RESTFUL way?

NOTE, currently for DELETE in a webpage, what i do is i use FORM tag with POST as action but include a _method with the value DELETE since this is what was indicated by others in SO on how to do RESTful delete for webpage.

+5  A: 

One option is to create a delete "transaction". So you POST to something like http://example.com/resources/deletes a new resource consisting of a list of resources to be deleted. Then in your application you just do the delete. When you do the post you should return a location of your created transaction e.g., http://example.com/resources/deletes/DF4XY7. A GET on this could return the status of the transaction (complete or in progress) and/or a list of resources to be deleted.

rojoca
What if after the POST and it is successful, i just want to go back to the previous page with the list of remaining items, but with a success message displayed in the page? how would this square with http://example.com/resources/deletes/DF4XY7 ?
keisimone
Well you can just redirect back to the collection (303 See Other). Perhaps you could add the delete transaction id in the query string e.g., http://example.com/resources?d=DF4XY7 then you can include some notification about the status of the given transaction i.e., success or failure.
rojoca
i dont quite get the idea of transaction. are you saying that i have to log in my database, whenever a database transaction is attempted?
keisimone
Nothing to do with your database. By transaction I just mean a list of operations to perform. In this case it is a list of deletes. What you do is create a new list (of deletes) as a resource in your application. Your web application can process that list however you want. That resource has a URI e.g., http://example.com/resources/deletes/DF4XY7. This means you could check on the status of the delete via a GET to that URI. This would be handy if when you did a delete you had to delete images from Amazon S3 or some other CDN and that operation might take a long time to complete.
rojoca
@rojoca i cannot visualise what you mean. So what do i see on the webpage for the URI example.com/resources/deletes/DF4XY7 just a success or failure message?
keisimone
+1 this is a nice solution. Instead of sending a DELETE to each resource, @rojoca proposes creating an instance of a new type of resource whose sole task is deleting a list of resources. For example, you have a collection of user resources and you want to delete Users Bob, Dave and Amy from your collection, so you create a new Deletion resource POSTing Bob, Dave and Amy as the creation parameters. The Deletion resource is created, and represents the asynchronous process of deleting Bob, Dave and Amy from the Users collection.
fd
I am sorry. I still have some slight difficulty in understanding a few issues.the DF4XY7. how on earth do you generate this string? This Deletion resource. Do i need to insert any data into the database?I apologise if i repeat some questions. It is just a little unfamiliar to me.
keisimone
I assume DF4XY7 is a generated unique id, perhaps it is more natural to just use the id generated when saved to the DB, for example example.com/resources/deletes/7. My take would be to create the Deletion model and save it in the database, you can have the asynchronous process deleting the other records update the Deletion model with the completion status, and any relevant errors.
fd
@fd is right; DF4XY7 is just a unique identifier similar to the one you had in the question. You can generate this however you like as long as it is unique.
rojoca
+2  A: 

I would say DELETE http://example.com/resources/id1,id2,id3,id4 or DELETE http://example.com/resources/id1+id2+id3+id4. As "REST is an architecture (...) [not] protocol" to quote this wikipedia article there is, I belive, no single one way of doing this.

I am aware that above is not possible without JS with HTML but I get the feeling that REST was:

  • Created without thinking of minor details like transactions. Who would need to operate on more then single item? This is somehow justified in HTTP protocol as it was not intended to serve through it anything else other then static webpages.
  • Not necessary well adjusting into current models - even of pure HTML.
Maciej Piechotka
A: 

As there is no 'proper' way to do this, what I have done in the past is:

send DELETE to http://example.com/something with xml or json encoded data in the body.

when you receive the request, check for DELETE, if true, then read the body for the ones to be deleted.

+2  A: 

I think rojoca's answer is the best so far. A slight variation might be, to do away with the javascript confirm on the same page, and in stead, create the selection and redirect to it, showing a confirm message on that page. In other words:

From:
http://example.com/resources/

do a

POST with a selection of the ID's to:
http://example.com/resources/selections

which, if successful, should respond with:

HTTP/1.1 201 created, and a Location header to:
http://example.com/resources/selections/DF4XY7

On this page you will then see a (javascript) confirm box, which if you confirm will do a request of:

DELETE http://example.com/resources/selections/DF4XY7

which, if successful, should respond with: HTTP/1.1 200 Ok (or whatever is appropriate for a successful delete)

fireeyedboy
I like this idea because you don't need any redirects. Incorporating AJAX you could do this all without leaving the page.
rojoca
After this DELETE http://example.com/resources/selections/DF4XY7, would i be redirected back to the example.com/resources?
keisimone
A: 

I am trying to perform a batch PUT using the following code. But I am getting a ClientHandlerException. Can anyone let me know how to use the Collection URI mentioned in wiki article for REST Server end code:

@PUT
    //@Path("{id}")
    @Consumes(MediaType.APPLICATION_XML)
    public List<Person> updatePerson( 
        final List<Person> person) throws RequestMediaTypeException, 
        RequestSemanticException, RequestSyntaxException, 
        ResourceConcurrencyException, ResourceGoneException, 
        ResourceNotFoundException, ResponseMediaTypeException,
        ResourceInternalServerException {
        return personResource.updatePerson(person);
    }

Client end code:

public static void main(String[] args) {
        String url = "http://localhost:8080/my-restful-ws/person";
        WebResource res = Client.create().resource(url);

        List<Person> personList = new ArrayList<Person>();
        Person customer = new Person();
        customer.setGuid("0");
        customer.setFirstName("Me");
        customer.setLastName("My");
        customer.setCreated(new Date());
        Person customer2 = new Person();
        customer2.setGuid("1");
        customer2.setFirstName("I");
        customer2.setLastName("IAM");
        customer2.setCreated(new Date());
        personList.add(customer);
        personList.add(customer2);


        ClientResponse putresponse = res.type("application/xml").put(ClientResponse.class, personList);
        System.out.println(putresponse);
    }

Response: Getting a client handler exception.Caused by: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class java.util.ArrayList, and MIME media type, application/xml, was not found

Radhika