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?
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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.