1

I am using a .php file to handle an HTTP delete request, and I would like to access the data sent with the delete request. I know the standard way to do this right now is:

parse_str(file_get_contents('php://input'),$_DELETE);

Then one can access the data via $_DELETE['dataName']. This is not working for me however, here is my exact code below:

else if($_SERVER['REQUEST_METHOD'] == "DELETE"){
    parse_str(file_get_contents('php://input'),$_DELETE);
    echo "contents of global DELETE variable:\n";
    echo var_dump($_DELETE);
    echo "contents of file get contents method:\n";
    echo file_get_contents('php://input');
}

This is what is printed out:

contents of global DELETE variable: array(1) { ["------------------------ 
----454596603246993785859143 Content-Disposition:_form-data;_name"]=> 
string(67) ""id" 5 ----------------------------454596603246993785859143-- 
" } 

contents of file get contents method: --------------------------- 
-454596603246993785859143 Content-Disposition: form-data; name="id" 5 --- 
-------------------------454596603246993785859143--

In this example is used "postman" to send a delete request to my php server with a generic id set to 5 as data. Instead of getting an associative array with "id=>5", I get a bunch of random data stored in an indexed array. Any help would be greatly appreciated as I have spent far too long trying to solve this issue.

Michael
  • 2,631
  • 2
  • 24
  • 40
NoobCoder
  • 11
  • 2

1 Answers1

1

I have tried this following way & it works perfectly in the Postman so you can try this,

else if($_SERVER['REQUEST_METHOD'] == "DELETE"){
    $output = json_decode(file_get_contents('php://input'), true);
    $id = $output['id'];
    echo $id;
}
Ropali Munshi
  • 2,757
  • 4
  • 22
  • 45