2

I have this custom service:

function zbs_services_resources() {
  return array(
    'productQuestionForm' => array(
      'actions' => array(
        'send' => array(
          'help' => 'save a FOO entity via send form',
          'file' => array('file' => 'inc', 'module' => 'zbs'),
          'callback' => '_zbs_productQuestionForm_create',
          //'access arguments' => array('note resource create'),
          //'access arguments append' => FALSE,
          'args' => array(
              array(
                'name' => 'foo',
                'type' => 'struct',
                'description' => 'The FOO to create',
                'source' => 'data',
                'optional' => false,
              ),
            ),
        ),
      )
    )

  );
}

And this little jQuery code in a simple html test file:

$.ajax({
    url: "/rest1/productQuestionForm/send",
    headers: { 'Content-Type': 'application/json','X-CSRF-Token':'MY_WORKING_TOKEN' },
    success: function (res) {
      alert("success");
      console.log(res);
    },
    data : {
      'foo' : 'pippo',
      "type":"page",
      "title":"Page submitted via entity JSON REST",
      "body": {
          "value": "<p>test</p>\n",
          "format": "filtered_html"
      }
    },
    type: 'post',
    dataType: 'json',
  });

But when I load the simple test html page I see this in developer suite for the POST url:

401 Unauthorized: Missing required argument foo

Where am I doing wrong?

1
  • Isn't the type mismatched from the service definition and the POST data?
    – mradcliffe
    Apr 20, 2016 at 20:53

2 Answers 2

1

The problem is the mismatch between the data type you define for foo ('struct'), and what you pass with JavaScript (a string). If you are going to pass a string, then your hook implementation should be the following one.

function zbs_services_resources() {
  return array(
    'productQuestionForm' => array(
      'actions' => array(
        'send' => array(
          'help' => 'save a FOO entity via send form',
          'file' => array('file' => 'inc', 'module' => 'zbs'),
          'callback' => '_zbs_productQuestionForm_create',
          //'access arguments' => array('note resource create'),
          //'access arguments append' => FALSE,
          'args' => array(
              array(
                'name' => 'foo',
                'type' => 'string',
                'description' => 'The FOO to create',
                'source' => 'data',
                'optional' => false,
              ),
            ),
        ),
      )
    )
  );
}

'source' => 'data' is perfectly fine, as shown in the example at hook_services_resources(), but notice what the documentation says.

In the case of 'data' unless you put things in an array, you will get passed to your functions. 'source' => array('data' => 'nid') will pass them off as a single variable; 'source' => array('data') will give you all argument values to each argument.

-1

Change

'source' => 'data',

to

'source' => array('data' => 'foo'),

Clear the caches, and it should work.

2
  • Sorry, but no. It does not work. Thanks, however.
    – ZioBudda
    Oct 2, 2014 at 20:28
  • did you clear cache and re-save your endpoint after making this change? Oct 15, 2014 at 6:34

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.