0

I'm trying to update a node via REST PUT but failing with the "404 controller not found" error. I have tried everything I know of but still getting the same error.

(No problem on retrieve / index)

Module:

function demoapp_feedback_update($edit, $account, $category) {
  cache_clear_all('demoapp_device_validate_pairing_' . $account->uid, 'cache', TRUE);
  cache_clear_all('demoapp_feedback_load_feedback_article_', 'cache', TRUE);
  cache_clear_all('demoapp_feedback_find_feedback_articles', 'cache', TRUE);
}

function demoapp_services_resources() {
  return array(
 'demoapp_feedback' => array(
  'retrieve' => array(
    'help' => 'Retrieves a feedback article',
    'file' => array('type' => 'inc', 'module' => 'demoapp', 'name' => 'resources/feedback_resource'),
    'callback' => 'demoapp_resources_feedback_retrieve',
    'access callback' => 'demoapp_resources_feedback_access',
    'access arguments' => array('retrieve'),
    'access arguments append' => TRUE,
    'args' => array(
      array(
        'name' => 'article_id',
        'type' => 'int',
        'description' => 'The id of the feedback article to get',
        'source' => array('path' => '0'),
        'optional' => FALSE,
      ),
      array(
        'name' => 'view_mode',
        'type' => 'int',
        'description' => 'Defines what data fields to return',
        'source' => array('param' => 'view_mode'),
        'optional' => TRUE,
        'default value' => 0,
      ),
    ),
  ),
  'index' => array(
    'help' => 'Retrieves a listing of feedback articles',
    'file' => array('type' => 'inc', 'module' => 'demoapp', 'name' => 'resources/feedback_resource'),
    'callback' => 'demoapp_resources_feedback_index',
    'access callback' => 'demoapp_resources_feedback_access',
    'access arguments' => array('index'),
    'access arguments append' => TRUE,
    'args' => array(
      array(
        'name' => 'view_mode',
        'type' => 'int',
        'description' => 'Defines what data fields to return',
        'source' => array('param' => 'view_mode'),
        'optional' => TRUE,
        'default value' => 0,
      ),
      array(
        'name' => 'checksum',
        'type' => 'string',
        'description' => 'Checksum of the local data',
        'source' => array('param' => 'checksum'),
        'optional' => TRUE,
        'default value' => NULL,
      ),
      array(
        'name' => 'offset',
        'type' => 'int',
        'description' => 'Offsets a list by X number of items',
        'source' => array('param' => 'offset'),
        'optional' => TRUE,
        'default value' => 0,
      ),
      array(
        'name' => 'limit',
        'type' => 'int',
        'description' => 'Limits a list by X number of items',
        'source' => array('param' => 'limit'),
        'optional' => TRUE,
        'default value' => 10000,
      ),
    ),
  ),
  'actions' => array(
    'update_poll' => array(
      'help' => 'Update a poll',
      'file' => array('type' => 'inc', 'module' => 'demoapp', 'name' => 'resources/feedback_resource'),
      'callback' => 'demoapp_resources_feedback_update_poll',
      'access callback' => 'demoapp_resources_feedback_access',
      'access arguments' => array('update_poll'),
      'access arguments append' => TRUE,
      'args' => array(
        array(
          'name' => 'node_id',
          'type' => 'int',
          'description' => 'The id of the node',
          'source' => array('data' => 'node_id'),
          'optional' => FALSE,
          'default value' => 0,
        ),
        array(
         'name' => 'vote',
         'type' => 'int',
         'description' => 'The vote chid',
         'source' => array('data' => 'chid'),
         'optional' => FALSE,
        ),
      ),
    ),
  ),
),

(Inc: nothing to see here)

Resource:

function demoapp_resources_feedback_update_poll($node_id, $chid) {
  global $user;
  global $demoapp_device;

  if (demoapp_validate_not_empty($node_id)) {
    if (!demoapp_validate_not_empty($node_id) || !demoapp_validate_not_empty($chid)) {
  services_error(RESOURCE_ERROR_400_MESSAGE, 400);
    }
  }
  //

  $retrievedVoteID = $chid;

  $node = node_load($node_id);
  $node_wrapper = entity_metadata_wrapper('node', $node);

  $selectedVote = $node_wrapper->poll[$chid]['chvotes'];
  $newVote = (int)$selectedVote + 1;

  $node_wrapper->poll[$chid]['chvotes']->set($newVote);
  $node_wrapper->save();

  watchdog('Poll update', 'Retrieved poll update query', $variables = array(
    'Node id' => $node_id,
    'Node object' => $node,
  ), $severity = WATCHDOG_NOTICE, $link = NULL);

  // Event (POLL_VOTE)
  $event = new demoappEvent();
  $event->type = 'POLL_VOTE';
  $event->name = NULL;
  $event->device_id = ($demoapp_device && $demoapp_device->device_id) ?       $demoapp_device->device_id : NULL;
  $event->user_id = $user->uid;
  $event->registered = time();
  $event->created = time();
  $event_id = demoapp_event_create_event($event);

  if (!$event_id) {
    services_error(RESOURCE_ERROR_500_MESSAGE, 500);
  }
  //

  return array('status' => 'SUCCESS:DONE');
}

function demoapp_resources_feedback_access($op, $args) {
  global $user;

  // Authenticate
  demoapp_services_authenticate();
  //

  $access = FALSE;

  switch ($op) {
    case 'retrieve':
      $access = user_access('access demoapp content');
      break;

    case 'index':
      $access = user_access('access demoapp content');
      break;

    case 'update_poll':
      $access = user_access('access demoapp content');
      break;
  }

  return $access;
}

Output:

Failed to load resource: the server responded with a status of 404 (Not found : Could not find the controller.)
  • Services & ops enabled
  • Index/retrieve works
  • Authenticated
  • Permissions set up properly

Thanks for your time.

Edit: JS function

postOpinion: function(data, onProgress, onSuccess, onAbort, onError){
    var httpHeaders = demoapp.auth.getHttpHeaders();

    var params = {
        node_id: data.nodeId,
        chid: data.vote
    };

    console.log('Params: ', params);

    demoapp.feedbackRemote.restClient.call(config.serverURL + '/' + config.serviceEndPoint + '/demoapp_feedback/update_poll', JSON.stringify(params), {requestMethod: 'PUT', requestDataType: 'application/json', httpHeaders: httpHeaders, responseDataType: 'json'},
        function _onProgress() {
            onProgress(demoapp.feedbackRemote.restClient);
        },
        function _onSuccess(result) {
            onSuccess(result.status);
        },
        function _onAbort() {
            onAbort();
        },
        function _onError(error) {
            console.log('Error: ', error);
            console.log('Sent url: ', config.serverURL + '/' + config.serviceEndPoint + '/demoapp_feedback/update_poll', JSON.stringify(params));
            onError(error);
        }
    );
},

1 Answer 1

1

PUT method is only work for creating item, but you didn't define create operation in hook_services_resources.

If you want to update node, please use POST method and define post operation instead.

function demoapp_services_resources() {
  return array(
  'demoapp_feedback' => array(
    'create' => array(
      // your custom code
    ),
    'update' => array(
      // your custom code
    ),
  );
}
0

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.