1

I'm trying to send a delete request to my PHP/Codeigniter api. Sending it from a NativeScript-Vue frontend.

 async deleteBackedupImages(identifiers) {
        console.log(identifiers);
        try {            
            var { data } = await axios({
                url: this.apiUrl + "/images?XDEBUG_SESSION_START=dsadsad",
                method: "delete",
                data:{
                    identifiers
                },
                headers: { "X-Requested-With": "XMLHttpRequest","Content-Type": "application/json" }
            });
            return data;
        } catch (error) {
            throw error;
        }
    }

On the PHP side of things, i have this function to take care of the JSON data:

function getJSONData():stdClass{
    try {
        $ci =& get_instance();
        $stream_clean = $ci->security->xss_clean($ci->input->raw_input_stream);
        $request = json_decode($stream_clean);
        return $request;
    } catch (\Throwable $th) {    
       throw $th;
    }

}

"identifiers" is just an array of strings.

$stream_clean variable comes out as an empty string, instead of JSON string.

I have to say it's a bit weird, that Axios docs state the following:

// data is the data to be sent as the request body // Only applicable for request methods 'PUT', 'POST', and 'PATCH'

I've seen on various posts, that a data object can actually be sent with a delete request.

What could be the problem with my code?

i.brod
  • 3,993
  • 11
  • 38
  • 74
  • _“I've seen on various posts, that a data object can actually be sent with a delete request.”_ - referring specifically to Axios, or just whether a DELETE request can have a body or not in general? What you quoted from the docs might simply indicate that Axios does not send a body when `method: "delete"` is specified, not matter whether you supply `data` or not … – 04FS May 31 '19 at 07:50
  • You know what the problem with your code is: you're trying to send a DELETE request with a body, which has no defined semantics. Even if Axios sends it, many server implementations ignore it. – jonrsharpe May 31 '19 at 07:50
  • 1
    Posts claiming that an Axios delete request can be sent with a body. Like this one:https://stackoverflow.com/questions/51069552/axios-delete-request-with-body-and-headers Also this one: https://github.com/axios/axios/issues/897 – i.brod May 31 '19 at 07:52
  • Well then maybe the server side part doesn’t care for a body on DELETE requests. Is this CodeIgniter? Have you researched whether that handles a body on DELETE requests the way you want to begin with? – 04FS May 31 '19 at 07:55
  • 04fs, i thought about this, but the interesting thing is that it works 100% when sent using Postman :D It's possible of course that "postman does something different", but i have no clue what. – i.brod May 31 '19 at 07:56

2 Answers2

3

Use params instead of body to send DELETE requests with Axios:

var { data } = await axios({
   url: this.apiUrl + "/images?XDEBUG_SESSION_START=dsadsad",
   method: "delete",
   params:{
      identifiers
   },
   headers: { "X-Requested-With": "XMLHttpRequest","Content-Type": "application/json" }
});

Note that according to Axios Documentation

// `params` are the URL parameters to be sent with the request
// Must be a plain object or a URLSearchParams object
0

For the php people out there. PHP doesn't parse the request body when doing a delete / put so the $_POST data stay empty. You can grab the input by using

file_get_contents('php://input'); 

thansk to get a PHP delete variable

Spent a whole afternoon trying to figure this out. In most cases it's probably easier to use the params option of axios. More details here: HTTP protocol's PUT and DELETE and their usage in PHP

Sjaak Wish
  • 455
  • 5
  • 8