Link to home
Start Free TrialLog in
Avatar of Pradip Shenolkar
Pradip ShenolkarFlag for India

asked on

How to use DELETE http method while deleting resources in Restful web service ?

I have read that we need to use DELETE http method while deleting resources in Restful Environment.
Till now I have managed to delete resources using  GET.
How do I use DELETE http method for deleting resources ?
I have tried following.
<form action="../de.pradip.jersey.todo/rest/todos" method="DELETE">	

Open in new window


Follow this site for more details : http://www.vogella.com/tutorials/REST/article.html
Avatar of mccarl
mccarl
Flag of Australia image

Firstly, no, "form" methods only support GET and POST, so you can't do delete like that. You can submit "DELETE" request via Ajax though. But how all that can work with what you are doing, we can't really say with the little amount of information that you've provided.

If you want further help, you will need to tell us more about what you are doing, such as what frameworks/libraries you are using on the client side (jQuery, etc) and what you are using on the server side (Spring, etc)? And any other detail that could be useful for us to help you out.
ASKER CERTIFIED SOLUTION
Avatar of Shalom Carmel
Shalom Carmel
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Pradip Shenolkar

ASKER

Hi shalomc

What I want exactly is, there is one simple application at following url.
But it allows only creating new resources. I want to implement deletion of resources in same application.
Could you please go through the link and help me achieving it ?

Go to point number 9.1 Project at below link. Skip point 9.5 because I am using html form to send request.

http://www.vogella.com/tutorials/REST/article.html
But it allows only creating new resources. I want to implement deletion of resources in same application.
That's not correct, it does allow for deletion of resources. Check out point 9.3, the code for the class TodoResource contains a method towards the bottom called deleteTodo that accepts the DELETE method.

Skip point 9.5 because I am using html form to send request.
If you will read again the previous comments that we both have made, you will see that we both said that submitting a delete request via HTML forms is not possible. You need to use ajax methods to do that as in the example given by shalomc.
Hi mccarl
You are absolutely right.
Since I am new to Restful web services, I don't  know how to use the code provided by shalomc in that application to get it work.
The example web service supports the DELETE method, see 9.3 point.
However, you cannot use DELETE directly from html form.
You will have to verify that DELETE works by using one of the methods I mentioned, or by any other client that supports DELETE. I suggest that you get acquainted with curl as a command line tool, as it is the best tool to debug and test everything http related, including web services.

This should work and delete todo #1.

curl -X DELETE http://localhost:8080/de.vogella.jersey.todo/rest/todos/1

or this in browser javascript will also delete todo #1
<script>
function execute($method,$url){ 
   xhr=new XMLHttpRequest(); 
   xhr.onreadystatechange = function() {
      if (xhr.readyState == 4) {
          return(xhr.responseText);
      }
   }
   xhr.open($method,$url,false) 
   xhr.send(null); 
   return(xhr.responseText);
} 

execute('DELETE','http://localhost:8080/de.vogella.jersey.todo/rest/todos/1");
</script>

Open in new window

@shalomc
I will try this.