[web] [PHP] Using HTTP DELETE

Started by
7 comments, last by bibiskuk 15 years, 10 months ago
I'm trying to setup a PHP application on my Dreamhost site to handle HTTP DELETE requests but whenever I test it the server bypasses my app and returns 503. Is there something that needs to be configured to allow DELETE to be used? I dont intend to have the server automatically delete the resource, I want to handle the deletion myself. For reference, and if it's important, PHP is being run using FastCGI. This is the simple test page I wrote as a test index.php

if ($_SERVER['REQUEST_METHOD'] != 'GET') {
   echo $_SERVER['REQUEST_METHOD'];
   //echo file_get_contents('php://input');
   exit;
}

echo <<<EOL
<html>
<head>
<title>test</title>
<script language="javascript">
   function sendMe() {
      var form = document.getElementById('test');
      var xmlHTTP = new XMLHttpRequest();
      var thing = document.getElementById('thing');
      
      function handler() {
         if (xmlHTTP.readyState == 4) {
            thing.innerHTML = xmlHTTP.responseText;
         }
      }
      
      xmlHTTP.onreadystatechange = handler;
      xmlHTTP.open("DELETE", "/", true);
      xmlHTTP.send("/phpinfo.php");
   }
</script>
</head>
<body>
<form id="test">
<textarea name="test"></textarea>
<input type="button" onclick="sendMe()" />
</form>
<div id="thing"></div>
</body>
</html>
EOL;
exit;

Advertisement
Ugh, looks like there are two problems here: 1) I suck at writing HTTP command directly via Telnet, 2) there seems to be a bug in Firefox where it doesnt handle the DELETE method properly for XMLHttpRequest.

I tried using cURL to use DELETE and it worked perfectly. I tried the example page in IE7 and Opera 9.5 and it worked perfectly.

Time to find or file a bug against Firefox.
Personally I'd use prototype for anything like this mainly because it takes into account cross-browser issues like this.

I found this resource, not sure if it helps:
http://dobrzanski.net/2007/04/22/using-put-and-delete-methods-in-ajax-requesta-with-prototypejs/
I actually did see that, but it actually uses POST to fakely send PUT and DELETE.
Silly question: Are you sure you have WebDAV working on your server?

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

I probably dont. WebDAV is setup to be whatever Dreamhost's default is. I'm trying to implement The Atom Publishing Protocol which makes no reference to WebDAV or other HTTP extensions, however.
Duh. My bad. I thought PUT and DELETE were part of WebDAV.

BTW, I found a nice bug here: http://opensourcesmall.biz/archives/2007/11/rest-php-put-and-webdav-on-apache/

Apparently there's a bug in the Apache's mod_dav that makes it respond to all PUTs and DELETEs on a server, so that they never pass on to PHP.

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

That's definitely something I'll need to keep in mind if I ever redeploy my code or start messing around with WebDAV. However, my PHP app is actually getting and processing PUT and DELETE properly - as long as it's not Firefox that's sending them. Even then, Firefox seems to only have trouble with DELETE.

That's not such a big issue since the idea of supporting the Atom Publishing Protocol is to make the site work with rich client apps.
I am working with Ruby on Rails, it uses a lot of PUT, and DELETE (for updating and deleting content). The problem is that most of the browser doesn't implement all the HTTP protocol. So to have PUT and DELETE to work on all navigators, Rails add an hidden field in his forms, with the real method.

<form action="/pages" method="post">  <input type="hidden" name="_method" value="put"/>  < !-- Rest of the form here -- <</form>


Hope it will help

[Edited by - bibiskuk on June 23, 2008 12:47:34 PM]

This topic is closed to new replies.

Advertisement