8

I'm using the Services module on Drupal 7. I want to implement a service that allows me to submit a search term via AJAX and receive an array containing the top 5 nodes (node id and title) that loosely match the title. When connecting to http://example.com/api/ I receive this message

Services Endpoint "search" has been setup successfully.

but when attempting to navigate to something like http://example.com/api/search/Test, I get a 404.

2
  • 1
    I suggest to check out RESTws, which will be part of D8 core.
    – the
    Aug 13, 2014 at 21:21
  • For future readers, consider using HTTP POST vs GET requests to retrieve data from the endpoint. Many services endpoints require POST requests. Aug 13, 2014 at 22:53

6 Answers 6

1

Later of try and try, when read from other tutorial, i make a resource with rest, soap for me is imposible in drupal, i try but without success, you can copy and page this code and modify only the custom module name and hooks.

/**
 * Implements hook_ctools_plugin_api().
 */
function core_custom_webservice_ctools_plugin_api($owner, $api) {
  if ($owner == 'services' && $api == 'services') {
    return array(
      'version' => 3,
      'file' => 'core_custom_webservice.services.inc'
    );
  }
}


function core_custom_webservice_services_resources() {
  $resources = array(
    'webservice_resources' => array(
      'operations' => array(
        'retrieve' => array(
          'help' => t('Response of webservice'),
          'file' => array('type' => 'inc', 'module' => 'core_custom_webservice', 'name' => 'core_custom_webservice.resource',),
          'callback' => '_core_custom_webservice_get_response',
          'access callback' => '_core_custom_webservice_access',
          'access arguments' => array('view'),
          'access arguments append' => TRUE,
          'args' => array(
            array(
              'name' => 'parameters',
              'type' => 'string',
              'description' => 'The parameters that define requested data',
              'source' => array('path' => '0'), // first argument in the url 
              'optional' => FALSE,
            ),
          ),
        ),
      ),
    ),
  );
  return $resources;
}

/* * *************************************************************************************************
 * Access callback 
 * For now only view/retrieve is implemented and if the user is logged in, he gets access
 */

function _core_custom_webservice_access($op) {
  global $user;
  $access = TRUE;

  switch ($op) {
    case 'view':
      if ($user->uid) {
        $access = TRUE;
      }
      break;
  }

  return $access;
}

function _core_custom_webservice_get_response($arg) {
  $response = 'something';
  return 'print '.$response;
}

try again in your service path and success with http://path.come/?q=webservice_server_rest/webservice_resources/string.json

0

If you created a RESTful Resource with the Services module, then I believe it will expect to respond to HTTP requests in JSON format, therefore you could try navigating to http://example.com/api/search/Test.json

0

Try to use this module https://www.drupal.org/project/services_views . It has currently two features: - Create view based resource creating Services display in a view - Execute any view of the system via views resource call

0

basically 404 is not found error

  1. Make sure that your service end point is

    http://example.com/api/search/{search-term}

  2. what is the response body to be send is in correct format

3.the content headers is set to application/json and name Content-Type

0

Hi First thing you have to do is

STEP 1: create a custom module and implement hook_services_resources()

EXAMPLE:

function mymodule_services_resources() {
  return array(
    'search' => array(
      'create' => array(
        'help' => 'Search for a content',
        'file' => array('file' => 'inc', 'module' => 'your module name'),
        'callback' => '_function_to_call_when_this_service_is_called',
        'access callback' => 'user_access',
        'access arguments' => array('access content'),
        'access arguments append' => FALSE,
        'args' => array(
         array(
           'name' => 'data',
           'type' => 'struct',
           'description' => 'The id of the data to get',
           'source' => 'data',
           'optional' => FALSE,
         ),
       ),
      ),
     ),
  );
}

STEP 2: go to

Structure -> services

and enable your module

STEP 3: sending data to the client $data - data sent to client it should be array

  $url = $base_url.'/api/search';
            $response = drupal_http_request($url, array(
                'headers' => array('Content-Type' => 'application/json', 'Accept' => 'application/json'),
               'method' => 'POST',
               'data' => json_encode($data),
               'max_redirects' => 0,
               )
             );

print the response to check if its working fine.

CHECK LIST:

  1. check for any log in by enabling DB logging module call your service.
  2. check for any syntax error because that file is called only when services is call until then you wont see any error or warning.
  3. print the response and test the data is passing correctly or not.
  4. check the client end to check.
0

I was getting this error too and then I realized I needed to include both the path of my service endpoint and the path of my View in the URL.

Originally I was trying http://my.local/events but I needed to be using http://my.local/events/events (I had named my service endpoint path the same as my View).

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.