I can create articles via REST without problems. But I can not figure out how to submit files.

How do I submit files (images) to Drupal 8 via REST?


REST UI - File: /file/{id}

Example image added in article: 
/admin/content/files -> /admin/content/files/usage/1

file {id} = 1?

Get file id 1:

curl --request GET --user admin:admin --header 'Accept: application/hal+json' http://d8.local/file/1
A fatal error occurred: No route found for "GET /file/1"

Post image (without entity content):

curl --request POST --user admin:admin --header 'Content-type: application/hal+json' http://d8.local/entity/file
{"error":"No entity content received."}

I can't find out how to create the entity content.

  • It looks like there are a couple of blockers for POSTing files. One is base64 encoding of files (drupal.org/node/1927648 patch available) and the other is REST endpoint permissions for Create Update Delete (drupal.org/node/2310307 patch available). I've not tested either of these yet. Commented Dec 8, 2014 at 6:34

3 Answers 3

  • Apply patch to the core: https://www.drupal.org/node/1927648
  • Install Rest UI using: drush dl restui-8.x-1.x
  • Go to /admin/config/services/rest and activate File /entity/file/{file}
  • Under /admin/people/permissions set all necessary permissions
  • Make sure files folder exists and has proper permissions
  • Encode image using command tool base64
  • POST to drupal.url/entity/file/

      "data":[{"value":"insert-output-from-base64-here"}] }

    enter image description here

  • When uploading a file successfully you get a 201, but nothing is returned pertaining to where the file is. I would expect an ID or location of the file to be returned. How do we reference the file once it is uploaded?
    – Ronnie
    Commented Jan 18, 2016 at 19:14
  • I modified EntityResource.php based off your patch and line 117, I returned the entity ID and I now at least have a reference to it that way: $response = new ResourceResponse(NULL, 201,['id' => $entity->id()]);
    – Ronnie
    Commented Jan 18, 2016 at 20:09
  • @Ronnie Based on the answer, I would expect the file to be located in your site's file folder. Commented Feb 8, 2016 at 17:02
  • 1
    @BlakeFrederick That doesn't really help me though if I need to save a reference to the file in the database. Think of this scenario: I have a content type an image field. An image needs to be associated with that node when created. When you upload the file, nothing is returned except a 201...what do I give that node to point to the image? That is why I added the $entity->id() to the response. I pass the entity id to the node and now that node knows about the image
    – Ronnie
    Commented Feb 8, 2016 at 17:06
  • @Ronnie - I'm just working through this myself, but I notice that when the file is successfully transmitted the server, a corresponding row is added to Drupal's file_managed table. This row has a unique fid. I believe the next step is to POST the creation of a new node with an image field and use the fid to associate the image field with the file. If I understand you, the question is how to determine the fid when nothing is returned. I noticed from a Drupal thread today that future version of the REST UI will return the full entity, but I'm not sure how to solve this problem for now. Commented Feb 11, 2016 at 4:45
  1. Install and enable RestUi and File Entity modules.
  2. Goto 'admin/config/services/rest' and enable File Resource with post method along with json and hal_json formats
  3. Enable post permissions Access POST on File resource @admin/people/permissions
  4. Post url - http://d8.local/entity/file?_format=hal_json
  5. Following format of post data (use base64 encoder for image data)
 "_links": {
   "type": {
     "href": "http://d8.local/rest/type/file/image"
 "filename": [
     "value": "favicon-32x32.png"
 "filemime": [
     "value": "image/png"
 "filesize": [
     "value": "488"
 "type": [
     "target_id": "image"
 "data": [
     "value": "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgBAMAAACBVGfHAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAJcEhZcwAACxMAAAsTAQCanBgAAAAkUExURUxpcSOq4SOq4SOq4SOq4SOq4SOq4SOq4SOq4SOq4SOq4SOq4U0m8UcAAAAMdFJOUwD/EvAm2qhTPMRrkl4CMYAAAAE1SURBVCjPXZG/S8NQEMePR7Ehnb48khC7BEFFnYJD6/aWOLilVBy6WBVc648/oCii1KlFKDrFwV2tqH+edy9Rk7zh8e7Dfe++944oP5dUO59RDQyPqrFK/WqKCzxUwA30oJyymervlVK8PsVBOd85hh6pyflfhwXg024cjguwBqCjhsCsSOAn+k4MBGpUWAB6Tb6W1VxAgv+MfcPgWUBgazjxoYwlQLrsmBa6MhbH3hep2yu6QJuowWL9Yfs1U4QRSbWOjRts0DMCdEbqjl8s1kYkPqmX7YGRfgy4aJe2IB+0wZJI2s7ojEGbWtJejL1aL55h+wGDVWTWix47+cAug6kAycjs+LkkjFzku0h6dC3D01KxCmdOLrt7pyQ0+Z/tRXT/eEr0dvL7y6zkrbhPtYVPrOAHKZQ53IyepQMAAAAASUVORK5CYII="
  1. Set Authorization ,Content-Type in headers.(Authorization: Basic xxxxxxxx Content-Type: application/hal+json)

  2. Use following php curl

    $curl = curl_init();
    curl_setopt_array($curl, array(
      CURLOPT_URL => "http://d8.local/entity/file?_format=hal_json",
      CURLOPT_TIMEOUT => 30,
      CURLOPT_POSTFIELDS => "{\n  \"_links\": {\n    \"type\": {\n      \"href\": \"http://d8.local/rest/type/file/image\"\n    }\n  },\n  \"filename\": [\n    {\n      \"value\": \"favicon-32x32.png\"\n    }\n  ],\n  \"filemime\": [\n    {\n      \"value\": \"image/png\"\n    }\n  ],\n  \"filesize\": [\n    {\n      \"value\": \"488\"\n    }\n  ],\n  \"type\": [\n    {\n      \"target_id\": \"image\"\n    }\n  ],\n  \"data\": [\n    {\n      \"value\": \"iVBORw0KGgoAAAANSUhEUgAAACAAAAAgBAMAAACBVGfHAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAJcEhZcwAACxMAAAsTAQCanBgAAAAkUExURUxpcSOq4SOq4SOq4SOq4SOq4SOq4SOq4SOq4SOq4SOq4SOq4U0m8UcAAAAMdFJOUwD/EvAm2qhTPMRrkl4CMYAAAAE1SURBVCjPXZG/S8NQEMePR7Ehnb48khC7BEFFnYJD6/aWOLilVBy6WBVc648/oCii1KlFKDrFwV2tqH+edy9Rk7zh8e7Dfe++944oP5dUO59RDQyPqrFK/WqKCzxUwA30oJyymervlVK8PsVBOd85hh6pyflfhwXg024cjguwBqCjhsCsSOAn+k4MBGpUWAB6Tb6W1VxAgv+MfcPgWUBgazjxoYwlQLrsmBa6MhbH3hep2yu6QJuowWL9Yfs1U4QRSbWOjRts0DMCdEbqjl8s1kYkPqmX7YGRfgy4aJe2IB+0wZJI2s7ojEGbWtJejL1aL55h+wGDVWTWix47+cAug6kAycjs+LkkjFzku0h6dC3D01KxCmdOLrt7pyQ0+Z/tRXT/eEr0dvL7y6zkrbhPtYVPrOAHKZQ53IyepQMAAAAASUVORK5CYII=\"\n    }\n  ]\n}",
      CURLOPT_HTTPHEADER => array(
        "authorization: Basic " .base64_encode('User:Password'),
        "cache-control: no-cache",
        "content-type: application/hal+json"
    $response = curl_exec($curl);
    $err = curl_error($curl);
    if ($err) {
      echo "cURL Error #:" . $err;
    } else {
      echo $response;
  3. Go and check file here - admin/content/files


You could install the REST UI module to see what endpoints you can use.

Very basic user interface for Drupal 8's REST module.

As it also let you configure what resources are exposed by what authentication.

Please note the UI currently provide the wrong endpoints:- REST URI paths changed to canonical paths

  • 1
    Rest UI endpoint for file: /file/{id}. I have updated the question.
    – user32063
    Commented Jul 3, 2014 at 10:59

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.