Can´t insert data in POST method

Hi, i am very new on this Frame, i am creating REST Api and now i am trying to insert data in a MYSQL DB with the POST method, but i can´t this is mi structure method:

$app->post(’/api/clientes/nuevo’, function (Request $request, Response $response) {

$nombre = $request->getServerParams('nombre'); //String
$apellidos = $request->geServerParams('apellidos');
$telefono = $request->getServerParams('telefono');
$email = $request->getServerParams('email');
$direccion = $request->getServerParams('direccion');
$ciudad = $request->getServerParams('ciudad');

$sql = "INSERT INTO clientes(nombre, apellidos, telefono, email, direccion, ciudad) VALUES (:nombre, :apellidos, :telefono, :email, :direccion, :ciudad)";

try {
    $db = new db();
    $db = $db->conectDB();
    $resultado = $db->prepare($sql);

    $resultado->bindParam(':nombre', $nombre);
    $resultado->bindParam(':apellidos', $apellidos);
    $resultado->bindParam(':telefono', $telefono);
    $resultado->bindParam(':email', $email);
    $resultado->bindParam(':direccion', $direccion);
    $resultado->bindParam(':ciudad', $ciudad);


   $response->getBody()->write("Nuevo cliente añadido");
   return $response;

    $resultado = null;
    $db = null;

} catch (PDOException $e) {

    echo '{"error" : {"text":' . $e->getMessage() . '}';


And this is my DB connection:

class db{

private $dbHost = 'localhost';
private $dbUser = 'root';
private $dbPass = '';
private $dbName = 'apirest';

public function conectDB(){
    $mysqlconnect = "mysql:host=$this->dbHost;dbname=$this->dbName";
    $dbConnecion = new PDO($mysqlconnect,$this->dbUser, $this->dbPass);
    $dbConnecion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    return $dbConnecion;


I think one of the manin problems is how i want access to the data or how a define the Request

I’m new to Slim so cut me some slack if this is wrong.

I think you’re looking for something like this.

// Collect input from the HTTP request
$data = (array)$request->getParsedBody();

$nombre = $data['nombre'];
$apellidos = $data['apellidos';
// and all the rest of the inputs

The rest is fine as far as I can tell.

Thanks so, I made that changes, and now when I send the request In my api cliente I recive this response:

Uncaught Slim\Exception\HttpMethodNotAllowedException: Method not allowed. Must be one of: GET

I think this problem gives me cause I am using post and get to recive the data, I explore some topics here and found that maybe I have to Setting CORS. But I`m not sure