I've been able to use restws to do a get, and a post, but not a put (an update).

I get this error:
EntityMetadataWrapperException: Invalid data value given. Be sure it matches the required data type and format. in EntityDrupalWrapper->set() (line 744 of /Users/barnettech/Sites/belldev/git/dev/babsoncomm/docroot/sites/all/modules/contrib/entity/includes/entity.wrapper.inc).

I looked through the issue que and saw the other 2 issues that were like this one. I applied the patch, but it didn't fix the problem.

// First get the uid for jbarnett so we can update his record (we'll need the
// uid.
$url1 = "http://bell7:8888/user.json?name=jbarnett";
$uid = get_username_uid($url1);

$result = update_user($uid);

function get_username_uid($url){
  echo 'got here';
  //global $user;
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL,"http://bell7:8888/user/login");
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_USERAGENT, 'PHP script');
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_COOKIEJAR, "session_cookie.txt");
  curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
  curl_setopt($ch, CURLOPT_POST, TRUE);
  curl_setopt($ch, CURLOPT_POSTFIELDS, "name=username&pass=password&form_id=user_login");
  curl_setopt($ch, CURLOPT_VERBOSE, true);
  curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());
  ob_start();  //dont print out this output
  curl_exec ($ch);
  curl_setopt($ch, CURLOPT_URL, "http://bell7:8888/restws/session/token");
  $HTTP_X_CSRF_TOKEN = curl_exec($ch);
  ob_end_clean();  //resume printout output to screen
  curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // output to command line
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'X-CSRF-Token: ' . $HTTP_X_CSRF_TOKEN));

  $ret = new stdClass;
  $ret->response = curl_exec($ch); // execute and get response
  $ret->error    = curl_error($ch);
  $ret->info     = curl_getinfo($ch);
  // uncomment the below line to get more verbosity on response;
  $user_info = array();
  $user_info = json_decode($ret->response);
  echo '**************';
  print_r($user_info);
  echo '**************';



curl_close ($ch);

unset($ch);

return $user_info->list[0]->uid;
}

function update_user($uid){
  $url = "http://bell7:8888/user/22.json";
  // data to update:
  $data = array('field_aboutme' => 'I am good');
  $data_encoded = json_encode($data);
  echo '***************';
  echo $data_encoded;
  echo '***************';


  //global $user;
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, "http://bell7:8888/user/login");
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_USERAGENT, 'PHP script');
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_COOKIEJAR, "session_cookie.txt");
  curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
  curl_setopt($ch, CURLOPT_POST, TRUE);
  curl_setopt($ch, CURLOPT_POSTFIELDS, "name=username&pass=password&form_id=user_login");
  curl_setopt($ch, CURLOPT_VERBOSE, true);
  curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); // output to command line
  ob_start();  //dont print out this output
  curl_exec ($ch);
  curl_setopt($ch, CURLOPT_URL, "http://bell7:8888/restws/session/token");
  $HTTP_X_CSRF_TOKEN = curl_exec($ch);
  ob_end_clean();  //resume printout output to screen

  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HTTPGET, FALSE);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  //curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: PUT'));
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data_encoded);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); // output to command line
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'X-CSRF-Token: ' . $HTTP_X_CSRF_TOKEN));

  $ret = new stdClass;
  $ret->response = curl_exec($ch); // execute and get response
  $ret->error    = curl_error($ch);
  $ret->info     = curl_getinfo($ch);
  print_r($ret);

curl_close ($ch);

unset($ch);

return;
}

I'm getting the following watchdog error:
Location: http://bell7:8888/user/22.json
Message: EntityMetadataWrapperException: Invalid data value given. Be sure it matches the required data type and format. in EntityDrupalWrapper->set() (line 744 of /Users/barnettech/Sites/belldev/git/dev/babsoncomm/docroot/sites/all/modules/contrib/entity/includes/entity.wrapper.inc)

I tried applying patch http://drupal.org/node/1956752 but this did not help. I've been tracing through the code, but I'm still unable to figure out exactly what the issue is.

Comments

barnettech’s picture

I've discovered that doing a put, my code above only works when I use $url = "http://bell7:8888/user/22";

and NOT

$url = "http://bell7:8888/user/22.json";

I assume this is a bug, as I read that the backbone project asked that the first case be allowed (I recently read through that issue), and I believe the readme says that it should work with the first case as well.

Anyhow I have my code working now, but thought to give you what I found -- that PUTS don't work if the .json extension is used in the CURLOPT_URL

awesome module. thanks for all your hard work.

Marko B’s picture

Nice to know that. I also kept getting the above error when trying to use PUT on .json file. when removed. It works.

lokapujya’s picture

Status: Active » Needs work

Indeed, the readme says that this should work. So, lets either fix it or update the readme.