How to test sending a JSON using Web Service by POST in php?

0

It turns out that I have the following method where I receive a JSON and get the values of its items by sending to save to the fields of a database

Method that receives JSON

    <?php
    require 'meta.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $body = json_decode(file_get_contents("php://input"), true);
        $retorno = meta::insert(
                $body['titulo'],
                $body['descripcion'],
                $body['fechaLim'],
                $body['categoria'],
                $body['prioridad']);

        if ($retorno) {
            print json_encode(array(
                 'estado'   => '1',
                 'mensaje'  => 'Creacion exitosa'
                ));
        }
        else
        {
            print json_encode(array(
                    'estado'    =>  '2',
                    'mensaje'   =>  'Creación fallida'
                )); 
        }
    }?>

in the meta.php file, I have the following insert method to send to the database.

public static function insert($titulo,$descripcion,$fechaLim,$categoria,$prioridad)
    {
        $comando = "INSERT INTO meta(".
                   "titulo,".
                   "descripcion,".
                   "fechaLim,".
                   "categoria,".
                   "prioridad)".
                   "VALUES( ?,?,?,?,?)";
        $sentencia = database::getInstance()->getDb()->prepare($comando);


        return $sentencia->execute(
            array(
                    $titulo,
                    $descripcion,
                    $fechaLim,
                    $categoria,
                    $prioridad
                )
            );
    }

I have the question of How to send the parameters by web service to save in the database? I'm using postman to test but I do not know what way to do it.

When performing the test in postman gives the following error:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE [23000]: Integrity constraint violation: 1048 Column' title 'can not be null' in C: \ xampp \ htdocs \ i_wish \ php \ meta.php: 67 Stack trace: # 0 C: \ xampp \ htdocs \ i_wish \ php \ meta.php (67): PDOStatement-> execute (Array) # 1 C: \ xampp \ htdocs \ i_wish \ php \ obtain_metas.php (59): meta :: insert (NULL, NULL, NULL, NULL, NULL) # 2 {main} thrown in C: \ xampp \ htdocs \ i_wish \ php \ meta.php on line 67

    
asked by JG_GJ 11.10.2017 в 06:41
source

1 answer

1
  • You select POST as the shipping method
  • You go to BODY and select form-data (form data is the equivalent of filling out the fields in a way Post uses for submission).
  • You add your values in the Key and Value columns
  • Press SEND , as always
  • EDITION:

    To send JSON, you must change steps 2 and 3:

  • You go to BODY and select raw , and there's a dropdown, where you select JSON (application / json)
  • Capture your data in JSON format
  • { "titulo":"", "descripcion":"", "fechaLim":"", "categoria":"", "prioridad":"" }

        
    answered by 11.10.2017 / 07:06
    source