Avatar of Pradip Shenolkar
Pradip Shenolkar
Flag 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
Web ServicesJava EEWeb Applications

Avatar of undefined
Last Comment
Pradip Shenolkar

8/22/2022 - Mon
mccarl

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
Shalom Carmel

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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
mccarl

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.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Pradip Shenolkar

ASKER
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.
Shalom Carmel

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

Pradip Shenolkar

ASKER
@shalomc
I will try this.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.