8

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?

Update:

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.

4
  • 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/

    {"_links":
    {
      "type":{"href":"http://drupal.url/rest/type/file/file"}
    },
      "filename":[{"value":"input.jpg"}],
      "filemime":[{"value":"image/jpeg"}],
      "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 Jan 18 '16 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 Jan 18 '16 at 20:09
  • @Ronnie Based on the answer, I would expect the file to be located in your site's file folder. – Blake Frederick Feb 8 '16 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 Feb 8 '16 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. – Blake Frederick Feb 11 '16 at 4:45
1
  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=" } ] }

  6. Set Authorization ,Content-Type in headers.(Authorization: Basic xxxxxxxx Content-Type: application/hal+json)

  7. Use following php curl

    $curl = curl_init();
    curl_setopt_array($curl, array(
      CURLOPT_URL => "http://d8.local/entity/file?_format=hal_json",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "POST",
      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);
    
    curl_close($curl);
    
    if ($err) {
      echo "cURL Error #:" . $err;
    } else {
      echo $response;
    }
    
  8. Go and check file here - admin/content/files

0

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.