lcpr_phoenix
lcpr_phoenix

Reputation: 443

Endpoint can't make a POST request properly in PHP using SLIM framework

I'm trying to make a API server that is supposed to connect to a data base, using SLIM framework following this tutorial. Actually I made a endpoint who get data from the data base, but can't insert new one.

Every time when I try to insert new data the POST parameters are nulls and the data base only send a error message.

In my groupes.php I have the following:

<?php

use Slim\Http\Request;
use Slim\Http\Response;

// Routes

$app->get('/[{name}]', function (Request $request, Response $response, array$args) {
   // Sample log message
   $this->logger->info("Slim-Skeleton '/' route");

   // Render index view
   return $this->renderer->render($response, 'index.phtml', $args);
});

$app->group('/api', function () use ($app) {

   $app->group('/v1', function () use ($app) {
       $app->get('/clients', 'getClients');
       $app->post('/make', 'addClient');
   });
});

The code above is just to define two endpoits: getClients and addClient.

And in my index.php I have:

function getConnection(){
    $dbhost = "127.0.0.1";
    $dbuser = "root";
    $dbpass = "dzpga883yRusPHhv";
    $dbname = "pruebaandroid";
    $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    return $dbh;
}

//method: GET
//domain: mi-api/api/v1/clients
function getClients($response){
    $sql = "SELECT * FROM cliente";
    try{
        $stmt = getConnection()->query($sql);
        $client = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;

        return json_encode($client);
    } 

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

//method: POST
//domain: mi-api/api/v1/make
function addClient($request) {
    $client = json_decode($request->getBody());

    $sql = 'INSERT INTO cliente (nombre, apellido, cedula, direccion, telefono, email) VALUES (:nombre, :apellido, :cedula, :direccion, :telefono, :email)';

    try {
        $db = getConnection();
        $stmt = $db->prepare($sql);
        $stmt->bindParam(":nombre", $client->nombre);
        $stmt->bindParam(":apellido", $client->apellido);
        $stmt->bindParam(":cedula", $client->cedula);
        $stmt->bindParam(":direccion", $client->direccion);
        $stmt->bindParam(":telefono", $client->telefono);
        $stmt->bindParam(":email", $client->email);
        $stmt->execute();
        $client->id = $db->lastInsertId();
        $db = null;
        echo json_encode($client);
    } 

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

So, using postman to do a GET request to mi-api/api/v1/clientes retrieve all the data as spected, but whhen I try with a POST request mi-api/api/v1/make?nombre=asdf&apellido=asdf&cedula=asdf&direccion=asdf&telefono=asdf&email=asdf it's supposed to insert the new information but postman give me the following error:

{"error":{"text":SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'nombre' cannot be null}}

Looks like the addClient function it's not geting the parameters, so it's taking it as null. Why is this happening? I don't know what is wrong here, any response is welcome.

See the image enter image description here

PD: yes, the DB is well formated, it have a id as PK auto increment and I tryed to print echo client->nombre and prints nothing but the error.

Upvotes: 0

Views: 570

Answers (1)

Pipe
Pipe

Reputation: 2424

You are adding your parameters as GET parameters (added on url).

Use the BODY -> form-data tab to specify your POST parameters (The same you have in PARAMS tab)

enter image description here

Upvotes: 1

Related Questions