Question

I'm having the following issue trying to create nodes from Drupal 7 to a Drupal 6 install. The target Drupal site is running Services 3.x.

Here's my code:

$base_url = $params['path'];
$data = array(
  'username' => $params['username'],
  'password' => $params['password'],
);
$data = http_build_query($data, '', '&');
$headers = array();
$options = array(
  'headers' => array('Accept' => 'application/json'),
  'method' => 'POST',
  'data' => $data
);

$response = drupal_http_request($params['path'] . '/user/login', $options);
$data = json_decode($response->data);

// Check if login was successful
if ($response->code == 200) {
  // Now recycle the login cookie we recieved in the first request
  $options['headers']['Cookie'] = $data->session_name . '=' . $data->sessid;

  // Create a new node
  $data = array(
    'node' => array (
      'type' => 'cloudnode',
      'title' => 'node1',
      'field_ip_address' => array ('value' => '1.2.3.4'),
    )
  );
  $options['data'] = http_build_query( $data, '', '&');
  $options['method'] = 'POST';
  $response = drupal_http_request($base_url . '/node', $options);
} 
else {
  die ('Failed to login');
}

I get a 200 response; the node is being created, but somehow it doesn't take care of my 'field_ip_address' CCK field at all.

I've tried any kind of variations but they don't work. There is not much documentation around as well.

If anyone has succeeded in creating nodes using REST, I would be very interested for any help.

Was it helpful?

Solution

CCK fields are always stored as an array of fields, even if the actual field is limited to only a single value.

So you it should be something like:

'field_ip_address' => array(array('value' => '1.2.3.4'))

Note that in Drupal 7, field values are additionally grouped by the language, which is 'und' (LANGUAGE_NONE) if the field is language-agnostic. So it would be something like this:

'field_ip_address' => array('und' => array(array('value' => '1.2.3.4')))

Copied from my comment, so that the question can be marked as fixed :)

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top