DELETE Request

As you may have guessed, an HTTP DELETE request is used to remove resources from some web service.

Since cURL is the de facto method for making HTTP requests in PHP, we will once again use it for our DELETE request:

<?php
$url = 'http://www.example.com/request.php';
$postvals = 'firstName=John&lastName=Smith';

$ch = curl_init($url);
$options = array(
   CURLOPT_CUSTOMREQUEST => 'DELETE',
   CURLOPT_URL => $url,
   CURLOPT_POST => 1,
   CURLOPT_POSTFIELDS => $postvals,
   CURLOPT_FOLLOWLOCATION => 1,
   CURLOPT_HEADER => 0,
   CURLOPT_RETURNTRANSFER => 1
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
curl_close($ch);
?>

In this case, we will send along some POST values to denote the resource to be deleted, so this request will very much mimic a PUT or POST. We simply set the CURLOPT_CUSTOMREQUEST to DELETE.

As with our PUT request, in Python we will use urllib2:

import urllib
import urllib2

url = 'http://www.example.com/request.py?id=1234'

request = urllib2.Request(url)
request.get_method = lambda : 'DELETE'

f = urllib2.urlopen(request)
response = f.read()

Our Python request will follow a pattern similar to our PUT request—we will need to manually set the get_method, this time to DELETE. We then make our request with urlopen(...) and read back the server response.

Get Programming Social Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.