hi,

Im trying to create a custom restAPI resource.

When implementing a custom GET function, i get results.
But when creating a post, i always get Method Not Allowed (Allow: GET)

Here is my post function, its simple

  public function post() {
    \Drupal::logger('test_module')->notice('post received');
  }

any help?

Comments

slewazimuth’s picture

Any kind of POST operation will show inclusion of 'entity' at the beginning of the endpoint. GET, PATCH, DELETE operations will not.
ie. If I have an entity with an id of zebra then the endpoint will show as /zebra/{zebra} for GET, PATCH, DELETE operations and /entity/zebra for the POST operation.

Most operations will likely require X-CSRF-Token header. Also make sure all operations are declared to REST with supported formats and authentication providers.

sensedrive’s picture

Hi,

i faced the same problem. I solved it by copying code from the EntityResource.php file in core (/core/modules/rest/src/Plugin/rest/resource/EntityResource.php).

/**
 * Represents entities as resources.
 *
 * @RestResource(
 *   id = "entity",
 *   label = @Translation("Entity"),
 *   serialization_class = "Drupal\Core\Entity\Entity",
 *   deriver = "Drupal\rest\Plugin\Deriver\EntityDeriver",
 *   uri_paths = {
 *     "canonical" = "/entity/{entity_type}/{entity}",
 *     "https://www.drupal.org/link-relations/create" = "/entity/{entity_type}"
 *   }
 * )
 *
 * @see \Drupal\rest\Plugin\Derivative\EntityDerivative
 */

I added an additional canonical path in order to get POST work.