Create file through services 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' =&gt; base64_encode($file),
        'filename' =&gt; $filename,
        'filepath' =&gt; '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' =&gt; 'test.user',
        'mail' =&gt; 'bogus_email@provider.com',
        'pass' =&gt; 'waytoolongboguspassword',
        'status' =&gt; 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);
    finfo_close($finfo);
    // 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' =&gt; $file-&gt;fid,
      'width' =&gt; 100,//set your own
      'height' =&gt; 100,//set your own
      'alt' =&gt; $args[1]['name'],//set your own
      'title' =&gt; $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