2

I have set up my drupal REST using REST Server and services. Also I have created my own module which implements hook_services_resources().

I have configured REST with to use my resource and node.

This works: http://local/rest/node/1

This does not work (returns 404): http://local/rest/hotel_search

Here is my module:

function MY_MODULE_services_resources() {
    return array(
        'hotel_search' => array(
            'retrieve' => array(
                'help' => 'Search for Hotels',
                'callback' => 'MY_MODULE_hotel_search',
                'access callback' => 'MY_MODULE_access_callback',
                'args' => array(
                    array(
                        'name' => 'name',
                        'description' => 'hotel name',
                        'type' => 'int',
                        'source' => 'param',
                        'optional' => false,
                    ),
                ),
            ),
        ),
    );
}

function MY_MODULE_access_callback() {
    return true;
}


function MY_MODULE_hotel_search() {
    $json = array(
        'name' => 'response',
        'status' => 'ok'
    );
    return json_encode($json);
}
  • Could it be that the names of the Access callbacks don't match? – Neograph734 May 5 '15 at 13:02
  • They are the same. I updated the code. – Michal Przybylowicz May 5 '15 at 13:03
  • Try using the actions array when declaring your resource, then use a POST to use it: drupalgap.org/node/187 - I believe the CRUD routing only works on the entity level, and not on the custom service resource level. – tyler.frankenstein May 5 '15 at 15:52
1

With the Services module, the "CRUD" resources are typically mapped only to entities, and routed to each based on the HTTP method:

GET -> Retrieve
POST -> Created
PUT -> Update
DELETE -> Delete

When building a custom service resource, you need to use the actions array when building your resource. In your example, your resources array would become:

return array(
  'my_module_resources' => array(
    'actions' => array(
        'hotel_search' => array(
            'help' => 'Search for Hotels',
            'callback' => 'MY_MODULE_hotel_search',
            'access callback' => 'MY_MODULE_access_callback',
            'args' => array(
                array(
                    'name' => 'name',
                    'description' => 'hotel name',
                    'type' => 'int',
                    'source' => 'param',
                    'optional' => false,
                ),
            ),
        ),
    ),
  ),
);

Your callback can just return an array or object, it doesn't need to encode the result, because Services will automatically do that based on the extension on the URL, e.g. .json.

You'd then make a call to it with a POST to:

?q=rest/my_module_resources/hotel_search.json
0

I do believe that, with the Services module, json_encode happens outside of the callback function you write.

Try to just return $json without encoding it, and see if that helps. If I'm correct, it will return JSON anyway.

  • Still got 404. Also response headers contain: Status Code: 404 Not found: Could not find the controller. – Michal Przybylowicz May 5 '15 at 13:34
  • At this point, you should be able to go into /admin/structure/services/list and see hotel_search and then retrieve under that. Is the Service enabled? – TerryCB May 5 '15 at 13:41
  • It's enabled there. – Michal Przybylowicz May 5 '15 at 16:49

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.