I want to POST json data to a custom REST resource. I tested with postman and Insomnia but i get the message as if would've done a GET request and returns the get functions result. The GET function works fine btw.
In the logs it says "Switched from post to get".
I just want to return the data the user passes. This is the code of my custom REST resource:
<?php
namespace Drupal\foolzz_rest_api\Plugin\rest\resource;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;
/**
* Provides User Profile REST Resource
*
* @RestResource(
* id = "profile_resource",
* label = @Translation("Profile resource"),
* uri_paths = {
* "canonical" = "/foolzz_rest_api/profile/{uid}"
* }
* )
*/
class ProfileResource extends ResourceBase
{
/**
* Responds to entity GET requests.
* @return \Drupal\rest\ResourceResponse
*/
public function get($uid = NULL)
{
$query = \Drupal::database()->select('foolzz_users_profile', 'data');
$query->fields('data', array('fk_users_uid','status','displayname','birthdate','biography','gender','realname'));
$query->condition('data.fk_users_uid', $uid);
$profile = $query->execute()->fetchAssoc();
$response = ['profile' => $profile
];
$build = array(
'#cache' => array(
'max-age' => 0,
),
);
return (new ResourceResponse($response))->addCacheableDependency($build);
}
/**
* Responds to POST requests.
* @return \Drupal\rest\ResourceResponse
* Returns a list of bundles for specified entity.
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
* Throws exception expected.
*/
public function post(array $data = []) {
$response = array(
"data" => $data,
);
$build = array(
'#cache' => array(
'max-age' => 0,
),
);
return (new ResourceResponse($response))->addCacheableDependency($build);
}
}