Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send a PUT/DELETE request in HTML?

Tags:

rest

html

forms

I'm making a REST API in PHP and I understand that I can capture the request method via $_SERVER['REQUEST_METHOD'].

However, how do I trigger PUT/DELETE requests in the browser?

I can't imagine changing the method attribute of the form tag to specify a method other than GET or POST would work on all browsers...plus the HTML standard doesn't recognize it.

Thanks.

like image 488
Housni Avatar asked Dec 02 '09 20:12

Housni


1 Answers

Usually this is done by a hidden form field, and handled in the application (not the webserver, by and large).

<input type="hidden" name="_method" value="put" />

So in this case you'd use a simple set of if-else statements to determine if the _method variable is overridden (validly, of course). I'd use something like:

$method = 'get';
if($_SERVER['REQUEST_METHOD'] == 'POST') {
  if(isset($_POST['_method']
     && ($_POST['_method'] == 'PUT' || $_POST['_method'] == 'DELETE')) {
    $method = strtolower($_POST['_method']);
  } else {
    $method = 'post';
  }
}

This would be a simple way to determine the request type for your application or framework.

like image 131
Robert K Avatar answered Sep 28 '22 04:09

Robert K