Drupal 8 - Upload file through REST API

Viewed 1417

I'm trying to upload a file to Drupal 8 through REST API. I enabled the file resource in REST resources.

This is what my code looks like in JS:

const uri = `${constants.URL}/entity/file?_format=json`;
const authorization = `Bearer ${session.accessToken}`;

var reqData = {
  "_links":{"type":{"href":`${constants.URL}/rest/type/file/image`}},
  "uri":{"value":"public://attachements/test.png"},
  "filename":{"value":"test.png"},
  "filemime":{"value":"image/png"},
  "type":{"target_id":"image"},
  "data":[{"value":"[base64]"}],
  "uid":[{"target_id":"1"}]
};

The result for the request is: Method Not Allowed. Or all sorts of strange things, depending on different things I try.

Inside User/Permissions I couldn't find anything to set to give permission.


Updated code -> 'The type link relation must be specified.'

const uri = `${constants.URL}/entity/file?_format=hal_json`;
const authorization = `Bearer ${session.accessToken}`;

var reqData = {
  "_links":{"type":{"href":`${constants.URL}/rest/type/file/image`}},
  "uri":[{"value":"public://test.png"},{"url":"/sites/default/files/test.png"}],
  "filename":{"value":"test.png"},
  "filemime":{"value":"image/png"},
  "type":{"target_id":"image"},
  "data":[{"value":`${base64Data}`}],
  "uid":[{"target_id":"1"}]
};

const response = await axios({
  method: 'POST',
  url: uri,
  data: {reqData},
  headers: {
    "Content-Type": "application/hal+json",
    "Accept": "application/hal+json",
    "Authorization": authorization
  }
})
1 Answers

Recently I also encountered this problem. Then I found the following solution:

  1. Install module File Entity
  2. Enable module HAL in Drupal core
  3. Go to Admin menu > Configuration > Web services > REST > Edit File resource > Enable hal_json format
  4. Set 'Content-Type': 'application/hal+json' in your request header

Response (using Postman):

{
    "fid": [
        {
            "value": 11
        }
    ],
    "uuid": [
        {
            "value": "c7c9185f-5394-4f65-90f1-aaa26de384ed"
        }
    ],
    "langcode": [
        {
            "value": "en"
        }
    ],
    "type": [
        {
            "target_id": "image",
            "target_type": "file_type",
            "target_uuid": "c2f5e03e-f726-4fc0-829f-8727c41b2b95"
        }
    ],
    "uid": [],
    "filename": [
        {
            "value": "test.jpeg"
        }
    ],
    "uri": [
        {
            "value": "public://test.jpeg",
            "url": "/sites/default/files/test.jpeg"
        }
    ],
    "filemime": [
        {
            "value": "image/jpeg"
        }
    ],
    "filesize": [
        {
            "value": 923812
        }
    ],
    "status": [
        {
            "value": true
        }
    ],
    "created": [
        {
            "value": "2021-03-12T15:22:50+00:00",
            "format": "Y-m-d\\TH:i:sP"
        }
    ],
    "changed": [
        {
            "value": "2021-03-12T15:22:50+00:00",
            "format": "Y-m-d\\TH:i:sP"
        }
    ],
    "field_image_alt_text": [],
    "field_image_title_text": []
}
Related