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?

5
  • “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 … Commented May 31, 2019 at 7: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. Commented May 31, 2019 at 7:50
  • 1
    Posts claiming that an Axios delete request can be sent with a body. Like this one:stackoverflow.com/questions/51069552/… Also this one: github.com/axios/axios/issues/897 Commented May 31, 2019 at 7: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? Commented May 31, 2019 at 7: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. Commented May 31, 2019 at 7:56

2 Answers 2

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 
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.