3

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);
    }
}
5

Looks like you've stumbled across the same stumbling block as I have a couple of times. Try with this piece of code - note the create entry after canonical:

 *   uri_paths = {
 *     "canonical" = "/foolzz_rest_api/profile/{uid}",
 *     "https://www.drupal.org/link-relations/create" = "/foolzz_rest_api/profile/{uid}"
 *   }
 * )

Read more at: https://www.drupal.org/node/2811757 and https://www.drupal.org/docs/8/api/restful-web-services-api/restful-web-services-api-overview

Oh and for the post request, I'd use ModifiedResourceResponse instead of fiddling with cache headers by hand (I learned about the existence of that from Drupal Core's EntityResource)

  • Thank you for your answer. I use this second line of uri_paths by myself in an extra rest resource which I'm only using to post data, because when I have these 2 lines in my resource with GET and POST then i receive this error : { "message": "No route found for \"POST /foolzz_rest_api/profile/1\": Method Not Allowed (Allow: GET, HEAD)" } – Towen Jun 5 '17 at 22:02
  • Hi! Have you remembered to enable POST for your endpoint at /admin/config/services/rest (if you use the Rest UI module)? – Bernt Andreas Drange Jun 8 '17 at 14:16
  • Sure. Post is enabled. – Towen Jun 9 '17 at 0:12
  • Right, so you have 1. the two urls defined AND 2. post allowed for that rest endpoint AND 3. you have granted permission for the role in question to post to this endpoint? Hmmmm scratching my head here. – Bernt Andreas Drange Jun 12 '17 at 12:25
3

I believe the syntax for your uri_paths annotation should be:

 *   uri_paths = {
 *     "canonical" = "/foolzz_rest_api/profile/{uid}"
 *     "https://www.drupal.org/link-relations/create" = "/foolzz_rest_api/profile"
 *   }

The {uid} is not needed on the second line as it is present in the canonical path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.