Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3715

CakePHP 5 get body data of POST request

$
0
0

I am creating a REST API using CakePHP 5. I have a POST function that should receive the data from postman and input it to the database. The problem is I am not receiving the data in the request. The GET request is working well so this is definitely not a database issue. The received data is being shown as an empty array [] with 200 as a response code. Thank you for your time.

routes.php

$routes->scope('/api', function (RouteBuilder $builder): void {    $builder->connect('/product/insert', ['controller' => 'Product', 'action' => 'insertProduct'])->setMethods(['POST']);});

ProductController.php

public function insertProduct() {    $product = $this -> Product -> newEmptyEntity();    $product = $this -> Product -> patchEntity($product, $this -> request -> getData());    $this -> Product -> save($product);}

Other things I have tried from the framework's documentation:

$product_name = $this -> request -> getData('product_name');$product = $this -> Product -> newEntity(['name' => $product_name,]);

$content = json_encode(['method' => __METHOD__, 'class' => get_called_class()]);
$data = $this->request->getParsedBody();
$product = $this -> Product -> newEntity($this->request->getData());
debug($this->request);

I also have Table and Entity classes:

ProductTable.php

namespace App\Model\Table;use Cake\ORM\Table;class ProductTable extends Table{      public function initialize(array $config): void      {           $this -> setTable('product');           parent::initialize($config);      }}

Product.php

namespace App\Model\Entity;use Cake\ORM\Entity;class Product extends Entity{}

Postman Snippet

enter image description here


Viewing all articles
Browse latest Browse all 3715

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>