I have defined few Drupal custom Services and they are working fine via POST requests. But one of them needs to be available via GET.

This one:

function rooms_services_services_resources() {
    return array(
        'api' => array(
            'actions' => array(
                'hotel_inventory' => array(
                    'help' => 'Inventory of all Hotels',
                    'callback' => 'rooms_services_hotel_inventory',
                    'access callback' => 'rooms_services_access_callback',
                    'access arguments append' => false,
                    'args' => array(
                        array(
                            'name' => 'params',
                            'optional' => true,
                            'source' => 'param',
                            'type' => 'struct',
                            'description' => 'all passed URL parameters'
                        ),
                    ),
                ),
            ),
        ),
    );
}

It's working fine when I am making a request via POST with headers:

`Content-Type: application/x-www-form-urlencoded`

or

`Content-Type: application/json`

as those I have enabled in my REST Server...

How do I make it work via simple GET request ?

up vote 1 down vote accepted

hook_menu() would work well for this. And it sounds like you have already worked out some of the functions, so given that, something like this might work:

function rooms_services_menu(){
  $items['api/hotel-inventory'] = array(
    'title' => 'All Hotel Inventory',
    'page callback' => 'rooms_services_hotel_inventory',
    'access callback' => 'rooms_services_access_callback',
  );
} 

function rooms_services_hotel_inventory(){
  ...
  return $inventory; //or whatever you want the GET request to return
}

I imagine you could put this in the same module as your REST hooks.

The short answer is you can't, unfortunately (AFAIK). For all non action resources, the Services module assumes that they are part of the entity C.R.U.D. spectrum, and then decides what to do based on the HTTP method. For example:

POST = Create
GET = Retrieve
PUT = Update
DELETE = Delete

So unless you're building an entity C.R.U.D. layer, you'll have to use POST on the actions array. Alternatively you could use hook_menu() to make your own custom page_callback that outputs JSON, then it could be accessed over GET. Another alternative is to use something like Views Datasource and compose a Views JSON to output your data (also available via GET).

rather than use an action , you could use the approach that is used in the documentation here: https://www.drupal.org/node/783460. Essentially just like you have the CRUD operations, create an 'index' operation.

Have a look at the node service too - that impliments an index service

call /endpoint/node - and it lists all nodes, it has parameters for fields, page size.

'index' => array(
      'help' => 'List all nodes',
      'callback' => 'rooms_services_hotel_inventory',
      'args' => array(

It seems the perfect fit for your inventory list.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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