DELETE method with parameter - 404 Not found [SOLVED]


#1

Hi there, I am currently in the process of learning how to buid my own REST API using the Slim v3. I found several tutorials and was able to build multiple routes to send GET and POST requests to my MySQL database. Next up for me is a delete request, but it doesn’t work.

This is my code:

$app->delete('/usuario/[{correo}]', function ($request, $response, $args) {
  $sth = $this->db->prepare("DELETE FROM usuarios WHERE email=:correo");
  $sth->bindParam("correo", $args['correo']);
  $sth->execute();
  $todos = $sth->fetchAll();
  return $this->response->withJson($todos);
});

I am testing it in Postman and I always have the same problem: 404 Not found. I can not understand it because I think the url is correct (http://localhost:8080/usuario/bbb@bbb.es).

Can anyone help me?


#2

That route works for me. (I didn’t setup a DB to test the database code.) I suspect it is something else in your environment that is causing the 404… Something in your .htaccess, other webserver config, or something within Postman.

App:

<?php

require __DIR__ . '/../vendor/autoload.php';

// Instantiate the app
$settings = require __DIR__ . '/../src/settings.php';
$app = new \Slim\App($settings);

$app->delete('/usuario/[{correo}]', function ($request, $response, $args) {
    return $this->response->withJson($args['correo']);
});

// Run app
$app->run();

Request:

## Request
curl -X "DELETE" "http://slim-skeleton.localhost:8080/usuario/bbb@bbb.es" \
     -H 'Content-Type: application/x-www-form-urlencoded; charset=utf-8' \
     -H 'Accept: application/json'

Response:

HTTP/1.1 200 OK
Server: Apache
Content-Type: application/json;charset=utf-8
X-Powered-By: PHP/7.0.14
Pragma: no-cache
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Date: Mon, 13 Nov 2017 15:27:29 GMT
X-Frame-Options: SAMEORIGIN
Keep-Alive: timeout=5, max=100
Content-Length: 12
Connection: Keep-Alive

"bbb@bbb.es"

#3

That is my problem I do not know why it does not work, because if I use the POST method, it works.

$app->post('/eliminar', function ($request, $response) {
  $input = $request->getParsedBody();
  $sql = "DELETE FROM usuarios WHERE email=:correo";
  $sth = $this->db->prepare($sql);
  $sth->bindParam("correo", $input['correo']);
  $sth->execute();
  $input['id'] = $this->db->lastInsertId();
  return $this->response->withJson($input);
});

#4

We seem to know it isn’t working because you are not getting to that route/action. Which webserver are you using? How is it configured? Have you tried the route from curl or another client?


#5

I am using XAMPP v7.1.6


#6

Okay, so you are using Apache…


#7

I have already found the problem.

You can not send dots as a character in the route URL, so you test http://localhost:8080/usuario/bbb@bbbdotes it will work fine.

Another way, you can send the email address in the body not in the url

{"email":"bbb@bbb.es"}

and then I can deal with it like POST route to fetch the email address as I did it before:

$app->post('/usuario, function ($request, $response) {
  $input = $request->getParsedBody();
  $sql = "DELETE FROM usuarios WHERE email=:correo";
  $sth = $this->db->prepare($sql);
  $sth->bindParam("correo", $input['correo']);
  $sth->execute();
  $input['id'] = $this->db->lastInsertId();
  return $this->response->withJson($input);
});

Thanks @tflight for your help.


#8

Perhaps describe that problem a bit more, and the solution you have for it, to teach future readers :slight_smile: