1

I'm attempting to create a custom rest resource, and have followed very many examples but I can't seem to get it to work. Every time I test it using https://www.hurl.it/ I just get a 301 error.

I'm using http://example.com/myrest_test/1?_format=json as the url as a GET request.

My format is as follows. I have a custom module called rest_test

Inside /rest_test/src/Plugin/rest/resource/MyRestTest.php I have

<?php

namespace Drupal\rest_test\Plugin\rest\resource;

use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;

/**
 * Provides a resource for accepting rest calls
 *
 * @RestResource(
 *   id = "myrest_test",
 *   label = @Translation("My Rest Test"),
 *   uri_paths = {
 *     "canonical" = "/myrest_test/{id}"
 *   }
 * )
 */

class MyRestTest extends ResourceBase {
    /**
     * Responds to GET requests.
     *
     *
     * @param int $id
     *   The ID.
     *
     * @return \Drupal\rest\ResourceResponse
     *   The response.
     */
    public function get($id = null) {
        $response = ['message' => 'Hey this worked and this is the id'.$id];
        return new ResourceResponse($response);
    }
}

and inside a form.js file I have

 (function($) {
    $(function() {
        console.log('form.js executed successfully');
        jQuery.ajax({
            url: "/myrest_test/5",  
        }).done(function(msg){
            console.log(msg);
        });
    })
 })(jQuery);

I receive the 'form.js executed successfully' message so I'm certain the javascript is running, but msg is empty. I've tried with the entire url instead of just the relative one as well. I've also made sure I'm clearing the cache But I can't seem to get any proper response. Is there something I'm missing? Why doesn't my rest resource response to my ajax calls?

UPDATE: Looking in the error logs it looks like I'm getting page not found errors for that rest call

UPDATE 2: When I visit directly in my browser I receive /myrest_test/{id}

{"message":"No route found for \u0022GET /myrest_test//234\u0022"}

UPDATE 3: After narrowing down the issue to it being a routing problem I found a similar question here No route found for custom REST resource however when I add

 *   uri_paths = {
 *     "canonical" = "/myrest_test/{id}",
 *     "https://www.drupal.org/link-relations/create" = "/myrest_test"
 *   }

I still encounter the same error

2
  • The error is in the redirect_404 module. Not directly related, but first you say the result is a 301 redirect, this error suggests, it is a 404 not found. Hard to tell what is really going on. What happens if you open the REST resource in a browser?
    – 4uk4
    Commented May 5, 2017 at 16:21
  • When I got rid of the redirect 404 module I just got the plain page not found error. When I visit it directly in my browser I get '{"message":"No route found for \u0022GET \/myrest_test\/\/234\u0022"}'
    – Amy
    Commented May 5, 2017 at 16:25

1 Answer 1

0

I'm not sure if there is a different lingering issue, but I was able to solve it by using the REST UI module.

https://www.drupal.org/project/restui

Once I installed it I was able to see my route listed on the configuration page, but it was listed as disabled. After enabling the route from the configuration page it started working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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