I've been trying to create and update users and their pictures through REST calls using the services module. So far, I've got creating and updating users to work, but creating files to set their profile pictures still doesn't work.

I try to do a POST request to my API endpoint (http://www.domain.com/api/api_name/file), with the following data:

    $data = array(
        'file' => base64_encode($file),
        'filename' => $filename,
        'filepath' => 'public://' . $filename

I POST this using CURL, with the exact same settings (apart from the API endpoint) as I'm using to create a user, which is working. However, I'm getting a HTML 500 as a response, with no response contents otherwise. The following error is logged in the Drupal error log:

Notice: Undefined property: stdClass::$fid in file_entity_access() (regel 1661 van /home/enschede/domains/aegee-enschede.nl/public_html/sites/all/modules/file_entity/file_entity.module).

The function that it errors on, file_entity_access, says that its $file parameter will either be an object, or a string with the file type (e.g. "image") if it is a create action. Apparently, it receives an object, so the create action is not executed properly on the server.

However, at this point I'm stuck. I can't find where these calls are coming from on the server, and I can't find anything I should change in the POST request. The fields I'm posting are the same as a tutorial I've followed. I'm posting the following fields to create a user, with the exact same CURL options otherwise:

    $initialData = array(
        'name' => 'test.user',
        'mail' => '[email protected]',
        'pass' => 'waytoolongboguspassword',
        'status' => 1

1 Answer 1


If you need to create/update a file alongside the entity posted JSON you need to create the file in your code like:

//Implements of hook_services_request_preprocess_alter().
function my_module_services_request_preprocess_alter($controller, $args, $options) {

switch ($controller['callback']) {
case '_user_resource_create':
case '_user_resource_update':
  if (isset($args[1]['avatar'])) {
    $data = base64_decode(trim($args[1]['avatar']));
    $finfo = finfo_open();
    $mime_type = finfo_buffer($finfo, $data, FILEINFO_MIME_TYPE);
    // get file type png. jpg
    $ext = $mime_type ? str_replace('image/', '', $mime_type) : 'png';
    $file_name = str_replace(' ', '-', $args[1]['name']);

    $file = file_save_data($data, 'public://users/' . $file_name . '.' . $ext, FILE_EXISTS_REPLACE);
    $args[1]['field_field_user_picture'] = array(
      'fid' => $file->fid,
      'width' => 100,//set your own
      'height' => 100,//set your own
      'alt' => $args[1]['name'],//set your own
      'title' => $args[1]['name']//set your own

By that way you could create any image field with any entity easily.

From the best practice I advice you to use the module user picture field that let's you deal with the user picture as a field so you could have it in views with different formats instead of just have the file id.

Another tip is to send the image format as It won't be possible to get the mime type from the json decoded file source.

Sure you still be able to create a file through file services but I think you have to make the post request format www/url-encoded instead of json

  • So it's not necessary to manually create the file first? I can post the image immediately alongside all other fields for the user?
    – Erik S
    Nov 25, 2015 at 10:36
  • Yes you could pass the image as a base64 image encoded and create a file using drupal api, And you still be able to create a file through file services but I think you have to make the post request format www/url-encoded instead of json Nov 25, 2015 at 10:46
  • Wait - Do you need to add a custom module with this hook, or will sending the image fields along with a user update over a REST call to services work by default?
    – Erik S
    Nov 25, 2015 at 11:47
  • Surely you will implement the hook in a custom module take a look at the file in sites/all/modules/services/docs/services.alter.api.php Nov 25, 2015 at 12:07

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.