• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 117
  • Last Modified:

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
0
Pradip Shenolkar
Asked:
Pradip Shenolkar
  • 3
  • 2
  • 2
1 Solution
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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.
0
 
shalomcCTOCommented:
If you consume the web service from a browser, then AJAX is necessary. Here is some example javascript code:
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://someurl..");

Open in new window


And here is an example using the command line curl tool
curl -X DELETE "http://someurl.."

Open in new window

0
 
Pradip ShenolkarAuthor Commented:
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
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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.
0
 
Pradip ShenolkarAuthor Commented:
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.
0
 
shalomcCTOCommented:
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

0
 
Pradip ShenolkarAuthor Commented:
@shalomc
I will try this.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now